3

このコードを使用して、これをphpファイルに送信しています。ファイルは次のようになります。

file_put_contents('dump.txt', "POST: \n" . print_r($_POST, true) . "\n\n\n GET: \n" . print_r($_GET, true));

私は次のようにjsonを送信しています:

public void postData(String url,JSONObject json) {
    HttpClient httpclient = new DefaultHttpClient();
    String object = "?data=" +json.toString();
    System.out.println("object:" + object);
    try {
        HttpPost httppost = new HttpPost(url.toString());
        httppost.setHeader("Content-type", "application/json");
        System.out.println("url:" + url.toString());
        StringEntity se = new StringEntity(object); 
        System.out.println("json:" + 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());
        System.out.println("response:" + response);

        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println("Statuscode " + statusCode);

        if(statusCode==200) {
            Toast.makeText(LbsGeocodingActivity.this, "Verstuurd",  Toast.LENGTH_LONG).show();
        }
        else if(statusCode!=200) {
            Toast.makeText(this, statusCode, Toast.LENGTH_LONG).show();
        }
        /* try {
                String responseBody = EntityUtils.toString(response.getEntity());
        Toast.makeText(this, responseBody, Toast.LENGTH_LONG).show();
        } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }*/
    } 
    catch (ClientProtocolException e) {

    } 
    catch (IOException e) {
    }
}

ログ ファイルは次のとおりです。

01-08 09:36:00.278: I/System.out(1124): object:?data={"id":"69403","timestamp":"08-01-2013 09:36:00","longitude":"-122.084095","latitude":"37.422005"}
01-08 09:36:00.298: I/System.out(1124): json:{"id":"69403","timestamp":"08-01-2013 09:36:00","longitude":"-122.084095","latitude":"37.422005"}
01-08 09:36:01.038: I/System.out(1124): response:org.apache.http.message.BasicHttpResponse@412d1b38
01-08 09:36:01.038: I/System.out(1124): Statuscode 200

セキュリティ上の理由から、URL を省略しました。

しかし、私はdump.txtに空のファイルを取得しています

POST: 
Array
(
)



 GET: 
Array
(
)

編集

setEntity (またはその周辺のどこか) に何か問題があります。URL に ?data= を既に貼り付けている場合、PHP ファイルからこれを取得するためです。

POST: 
Array
(
)



 GET: 
Array
(
    [data] => 
)
4

1 に答える 1

10

アプリでJSONを送信する方法はDefaultHttpClient完全に機能します。

public static HttpUriRequest createPostForJSONObject(
        JSONObject params, String url) {
    HttpPost post = new HttpPost(url);
    post.setEntity(createStringEntity(params));
    return post;
}

private static HttpEntity createStringEntity(JSONObject params) {
    StringEntity se = null;
    try {
        se = new StringEntity(params.toString(), "UTF-8");
        se.setContentType("application/json; charset=UTF-8");
    } catch (UnsupportedEncodingException e) {
        Log.e(TAG, "Failed to create StringEntity", e);
        exception = e;
    }
    return se;
}

コードでこのリクエストを実行するには、次のことを行う必要があります。

public void postData(String url,JSONObject json) {
    HttpClient httpclient = new DefaultHttpClient();
    try {
          HttpPost post = createPostForJSONObject(json, url);
          HttpResponse response = httpClient.execute(post);
          // DO YOUR THING
    } catch Exception {
         // HANDLE EXCEPTION
    }
}
于 2013-01-08T09:51:20.273 に答える