2

我正在尝试发布 json 对象,然后尝试接收 json 对象。通过嗅探器显示该帖子并获得成功的作品。但接收对象始终为空。我想这是因为我的粗心,所以我希望你能帮助我。

    try {
        DefaultHttpClient httpclient = new DefaultHttpClient(ccm, params);
        HttpPost httppostreq = new HttpPost(URL);

        StringEntity se;
        se = new StringEntity(jsonObjSend.toString());
        //se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        se.setContentType("application/json;charset=UTF-8");
        //se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json;charset=UTF-8"));
        httppostreq.setEntity(se);

        //Setting headers
        HttpResponse response = httpclient.execute(httppostreq);

        HttpEntity entity = response.getEntity();

        if (entity != null) {
            InputStream instream = entity.getContent();
            Header contentEncoding = response.getFirstHeader("Content-Encoding");
            if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
                instream = new GZIPInputStream(instream);
            }
            // convert content stream to a String
            String resultString= convertStreamToString(instream);
            instream.close();
            resultString = resultString.substring(1,resultString.length()-1);

            // Transform the String into a JSONObject
            JSONObject jsonObjRecv = new JSONObject(resultString);

            return jsonObjRecv;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;


private static String convertStreamToString(InputStream is) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line).append("\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

这是 Activity 的一部分,我在行中不断收到 NullPointerException 错误JSONObject jsonObjRecv = JSONPost.SendHttpPost(URL, jsonObjSend);

// Send the HttpPostRequest and receive a JSONObject in return
JSONObject jsonObjRecv = JSONPost.SendHttpPost(URL, jsonObjSend);
    try {
        jsonObjRecv.getJSONObject("id"); //Here I got the error
    } catch (JSONException e) {
        e.printStackTrace();
    }

当我将 jsonObjRecv 类型更改为 String 时,它开始工作,但我需要 JSON 类型。

错误:

02-01 21:39:01.673: ERROR/AndroidRuntime(20855): FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Phone/com.Phone.MainActivity.OtherActivity}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2781)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2797)
    at android.app.ActivityThread.access$2300(ActivityThread.java:135)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2132)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:143)
    at android.app.ActivityThread.main(ActivityThread.java:4914)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NullPointerException
    at com.Phone.MainActivity.OtherActivity.onCreate(OtherActivity.java:47)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1065)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2745)
    ... 11 more

谢谢您的答复。

4

3 回答 3

2

您在 JSONPost.SendHttpPost 中遇到异常,导致它返回 null。这意味着当您尝试对结果调用 getJSONObject 时,您会得到一个空指针异常。看起来您正试图通过 SendHttpPost 函数在 onCreate 中执行 HTTP 请求。这是不允许的——你不能在 UI 线程上做网络 IO,它会抛出一个异常。您需要在线程或 AsyncTask 中执行此操作。

于 2013-02-01T19:19:02.647 回答
0

检查这个。它将轻松帮助您获取和解析正确的 JSON 响应。具体来说,它为 JSON 请求提供了一个非常简洁的JsonHttpResponseHandler 。

于 2013-02-01T19:40:00.127 回答
0

HttpEntity 实体 = response.getEntity();'

这是返回您的任何数据吗?在使用响应对象之前在那里放置一个空检查。您还为 sendhttppost 中的异常返回 null

于 2013-02-01T21:00:34.270 回答