0

我想为 Android 和 iOs 构建 2 个相同的产品。iOs 已经可以工作了,但是 android 不行,那是因为字符串的格式。

在 iOs 中,这是:

NSString*jsonString = [[NSString alloc] initWithFormat:@"{\"id\":\"%@\",\"longitude\":\"%@\",\"latitude\":\"%@\",\"timestamp\":\"%@\"}", _phonenumber, longitude , latitude, stringFromDate];

而且我不知道如何在android中完全这样做。这里的格式不同。我现在拥有的是:

HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost(" http://www.myserver.nl/locate.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) {

} 

在此先感谢,我在本周末之前需要这个,所以如果你能帮助我,那将不胜感激

我从 iOs 字符串中得到这个结果:

{
    "id":"0612833398",
    "longitude":"-143.406417",
    "latitude":"32.785834",
    "timestamp":"10-10 07:56"
}

好的,这就是我的问题所在。

是的,我需要将这个确切的字符串发送到服务器上的 asp.net 文件。但我需要知道如何将其与此结合:与 http 帖子

HttpPost httppost = new HttpPost("http://www.myserver.nl/locatie.php");

在我像这样将它与 nameValuePPairs 结合起来之前

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
4

2 回答 2

2

通过创建一个 JosnObject 来创建与您在 IOS 中相同的字符串:

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"); 

现在,如果您为 json 对象打印,您将得到以下字符串:

{"id":"0612833398","longitude":"-143.406417","latitude":"32.785834",
                                                   "timestamp":"10-10 07:56"}

并将 JSONObject 发布到服务器:

  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) {

  } 
于 2013-01-03T12:24:44.513 回答
2

要创建一个字符串,您可以使用 String.format()。在这种情况下,语法与 Objective-C 非常相似:

String s = String.format("{\"id\":\"%s\",\"longitude\":\"%s\",\"latitude\":\"%s\",\"timestamp\":\"%s\"}", num, long, lat, time);

HTTP Post 是这样的:

HttpPost postMethod = new HttpPost(url);


try {

HttpParams params = new BasicHttpParams();

params.setParameter(name, value);


postMethod.setParams(params);

httpClient.execute(postMethod);


} catch (Exception e) {

} finally {

postMethod.abort();

}
于 2013-01-03T12:17:59.543 回答