0

AndroidとiOS用に同じ製品を2つ作成したいと思います。iOSはすでに機能していますが、Androidは機能しません。これは、文字列の形式が原因です。

iOSではこれは次のとおりです。

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

そして、私はこれをアンドロイドでこのように正確に行う方法を知りません。ここでの形式は異なります。私が今持っているのは:

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

} 

よろしくお願いします。週末までにこれが必要なので、助けていただければ幸いです。

私はこれを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

文字列を作成するには、String.format()を使用できます。この場合、構文はObjective-Cと非常によく似ています。

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

HTTPPostは次のようになります。

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 に答える
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 に答える