Android HttpClient将json对象发布到服务器并接收响应

时间:2015-10-30 11:24:51

标签: android json asynchronous

我在发布json数据后没有收到服务器的成功响应。它总是在onFailure中结束。但是webservice检索json数据。 这是我用来发送数据的代码。

    JSONObject addressData= new JSONObject();
    try {
        addressData.put("city", "Sydney");
        addressData.put("Country", "Australia");

    } catch (JSONException e) {
        e.printStackTrace();
    }

JSONObject JsonString = new JSONObject();
    JsonString.put("Address", addressData);

StringEntity entity = new StringEntity(JsonString.toString());

AsyncHttpClient httpClient = new AsyncHttpClient();
httpClient.post(getApplicationContext(),"URL", entity, "application/json", new JsonHttpResponseHandler() {

@Override
            public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
                super.onSuccess(statusCode, headers, response);
                System.out.println("onSuccess: " + response);
                }
@Override

            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse){
                if (statusCode == 404) {
                    Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
                } else if (statusCode == 500) {
                    Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
                } else {
                    System.out.println("onFailure" + throwable + "errorResponse: " + errorResponse);
                }
            }
 });

上面的代码将json数据发送到服务器,但它没有给onSuccess响应。

logcat的

1612-1617/com.example.ziad_pc.heyvote D/dalvikvm﹕ GC_CONCURRENT freed 1135K, 24% free 14611K/19207K, paused 15ms+1ms, total 17ms
10-30 14:52:59.931    1612-1617/com.example.ziad_pc.heyvote D/dalvikvm﹕ GC_CONCURRENT freed 1185K, 24% free 14607K/19207K, paused 11ms+1ms, total 14ms
10-30 14:53:00.481    1612-1617/com.example.ziad_pc.heyvote D/dalvikvm﹕ GC_CONCURRENT freed 1207K, 25% free 14581K/19207K, paused 14ms+1ms, total 16ms
10-30 14:53:01.021    1612-1617/com.example.ziad_pc.heyvote D/dalvikvm﹕ GC_CONCURRENT freed 1173K, 25% free 14579K/19207K, paused 16ms+0ms, total 19ms
10-30 14:53:01.561    1612-1617/com.example.ziad_pc.heyvote D/dalvikvm﹕ GC_CONCURRENT freed 1171K, 25% free 14579K/19207K, paused 14ms+0ms, total 17ms
10-30 14:53:02.132    1612-1617/com.example.ziad_pc.heyvote D/dalvikvm﹕ GC_CONCURRENT freed 1171K, 25% free 14580K/19207K, paused 13ms+1ms, total 15ms
10-30 14:53:02.681    1612-1617/com.example.ziad_pc.heyvote D/dalvikvm﹕ GC_CONCURRENT freed 1172K, 25% free 14579K/19207K, paused 17ms+0ms, total 20ms
10-30 14:53:03.231    1612-1617/com.example.ziad_pc.heyvote D/dalvikvm﹕ GC_CONCURRENT freed 1172K, 25% free 14579K/19207K, paused 13ms+0ms, total 15ms
10-30 14:53:03.761    1612-1617/com.example.ziad_pc.heyvote D/dalvikvm﹕ GC_CONCURRENT freed 1171K, 25% free 14579K/19207K, paused 18ms+1ms, total 20ms
10-30 14:53:04.291    1612-1617/com.example.ziad_pc.heyvote D/dalvikvm﹕ GC_CONCURRENT freed 1171K, 25% free 14579K/19207K, paused 19ms+0ms, total 22ms
10-30 14:53:04.812    1612-1617/com.example.ziad_pc.heyvote D/dalvikvm﹕ GC_CONCURRENT freed 1171K, 25% free 14580K/19207K, paused 20ms+1ms, total 22ms
10-30 14:53:05.341    1612-1617/com.example.ziad_pc.heyvote D/dalvikvm﹕ GC_CONCURRENT freed 1172K, 25% free 14579K/19207K, paused 15ms+0ms, total 17ms
10-30 14:53:05.861    1612-1617/com.example.ziad_pc.heyvote D/dalvikvm﹕ GC_CONCURRENT freed 1171K, 25% free 14579K/19207K, paused 16ms+0ms, total 19ms
10-30 14:53:06.392    1612-1617/com.example.ziad_pc.heyvote D/dalvikvm﹕ GC_CONCURRENT freed 1171K, 25% free 14580K/19207K, paused 17ms+1ms, total 19ms
10-30 14:53:06.902    1612-1617/com.example.ziad_pc.heyvote D/dalvikvm﹕ GC_CONCURRENT freed 1172K, 25% free 14580K/19207K, paused 11ms+1ms, total 13ms
10-30 14:53:07.421    1612-1617/com.example.ziad_pc.heyvote D/dalvikvm﹕ GC_CONCURRENT freed 1172K, 25% free 14580K/19207K, paused 20ms+0ms, total 22ms

0 个答案:

没有答案