在Android的正是创建此字符串 [英] Create this string exactly in Android
问题描述
我要建立2相同的产品,为Android和iOS。
iOS的已经工作,但Android的犯规,这是因为该字符串的格式。
在IOS是这样的:
的NSString * jsonString = [[NSString的页头] initWithFormat:@{\\ID \\:\\%@ \\,\\经度\\:\\%@ \\ ,\\纬\\:\\%@ \\,\\时间戳\\:\\%@ \\},_phonenumber,经度,纬度,stringFromDate]
,我不知道如何在Android的做到这一点exactely这样。这里的格式是不同的。
我现在拥有的是:
的HttpClient HttpClient的=新DefaultHttpClient();
HttpPost httppost =新HttpPost(http://www.myserver.nl/locatie.php); 尝试{
//添加数据
清单<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;(4); nameValuePairs.add(新BasicNameValuePair(ID,NUM));
nameValuePairs.add(新BasicNameValuePair(经度,茇));
nameValuePairs.add(新BasicNameValuePair(纬度,LAT));
nameValuePairs.add(新BasicNameValuePair(时间戳,时间));
httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
HTT presponse响应= httpclient.execute(httppost); }赶上(ClientProtocolException E){ }赶上(IOException异常五){ }
在此先感谢,我需要这在本周结束,所以如果你能帮助我,那将是极大的AP preciated
我得到这个作为一个结果,从IOS字符串:
{ID:0612833398,东经: - 143.406417,纬度:32.785834,时间戳:10-10 07:56}
块引用>好吧,这是我的问题是什么。
是的,我需要这个确切字符串发送到一个asp.net文件的服务器上。但我需要知道如何将此与此相结合:用HTTP POST到
HttpPost httppost =新HttpPost(http://www.myserver.nl/locatie.php);
块引用>在我这个组合,像这样的nameValuePPairs
httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
块引用>解决方案创建你在IOS通过创建一个JosnObject为获得相同的字符串:
JSONObject的JSON =新的JSONObject();
json.put(ID,0612833398);
json.put(经度,-143.406417);
json.put(纬度,32.785834);
json.put(时间戳,10-10 07:56);现在,如果你为JSON对象的打印你会得到这个字符串:
{ID:0612833398,东经: - 143.406417,纬度:32.785834
时间戳:10-10 07:56}和邮政的JSONObject作为服务器:
{尝试
HttpClient的HttpClient的=新DefaultHttpClient(); HttpPost httppost =新HttpPost(http://www.myserver.nl/locatie.php);
httppost.setHeader(内容类型,应用/ JSON);//这里创建JSON对象...
JSONObject的JSON =新的JSONObject();
json.put(ID,0612833398);
json.put(经度,-143.406417);
json.put(纬度,32.785834);
json.put(时间戳,10-10 07:56);///创建StringEntity当前JSON obejct StringEntity本身=新StringEntity(json.toString());
se.setContentEncoding(新BasicHeader(HTTP.CONTENT_TYPE,应用/ JSON));
httppost.setEntity(SE); HTT presponse响应= httpclient.execute(httppost);
字符串TEMP = EntityUtils.toString(response.getEntity()); }赶上(ClientProtocolException E){ }I want to build 2 same products for Android and iOs. The iOs already works, but the android doesnt, that's because of the format of the string.
in iOs this is:
NSString*jsonString = [[NSString alloc] initWithFormat:@"{\"id\":\"%@\",\"longitude\":\"%@\",\"latitude\":\"%@\",\"timestamp\":\"%@\"}", _phonenumber, longitude , latitude, stringFromDate];
And i dont know how to do this exactely like this in android. The format here is different. What i have now is:
HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://www.myserver.nl/locatie.php"); try { // Add your data List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4); nameValuePairs.add(new BasicNameValuePair("id", num)); nameValuePairs.add(new BasicNameValuePair("longitude", longi)); nameValuePairs.add(new BasicNameValuePair("latitude", lat)); nameValuePairs.add(new BasicNameValuePair("timestamp", time)); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); } catch (ClientProtocolException e) { } catch (IOException e) { }
Thanks in advance, i need this by the end of the week so if you could help me, that would be greatly appreciated
i get this as an result from the iOs string:
{"id":"0612833398","longitude":"-143.406417","latitude":"32.785834","timestamp":"10-10 07:56"}
Okay, this is what my problem is.
Yes i need to send this exact string to an asp.net file on a server. But i need to know how to combine this with this: with the http post to
HttpPost httppost = new HttpPost("http://www.myserver.nl/locatie.php");
before i combined this with the nameValuePPairs like this
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
解决方案Create same string as you are getting in IOS by create an JosnObject as:
JSONObject json = new JSONObject(); json.put("id", "0612833398"); json.put("longitude", "-143.406417"); json.put("latitude", "32.785834"); json.put("timestamp", "10-10 07:56");
now if you make a print for json object you will get this string :
{"id":"0612833398","longitude":"-143.406417","latitude":"32.785834", "timestamp":"10-10 07:56"}
and Post JSONObject as to server :
try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://www.myserver.nl/locatie.php"); httppost.setHeader("Content-type", "application/json"); // Create json object here... JSONObject json = new JSONObject(); json.put("id", "0612833398"); json.put("longitude", "-143.406417"); json.put("latitude", "32.785834"); json.put("timestamp", "10-10 07:56"); /// create StringEntity with current json obejct StringEntity se = new StringEntity(json.toString()); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); httppost.setEntity(se); HttpResponse response = httpclient.execute(httppost); String temp = EntityUtils.toString(response.getEntity()); } catch (ClientProtocolException e) { }
这篇关于在Android的正是创建此字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!