2013-02-01 143 views
2

我想發佈json對象,然後嘗試接收json對象。通過嗅探器顯示該帖子並獲得成功作品。但接收對象始終爲空。 我認爲這是因爲我的粗心大意,所以我希望你能幫助我。NullPointerException當返回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(); 
} 

,這裏是活動的一部分,在那裏我一直在行JSONObject jsonObjRecv = JSONPost.SendHttpPost(URL, jsonObjSend);得到NullPointerException異常錯誤:

// 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 

謝謝您的答覆。

+0

過去烏爾JSON響應異常 – MuraliGanesan

+0

你可以指出哪些是OtherActivity.java 47行 –

+0

@DavidM返回null,47行是:jsonObjRecv.getJSONObject( 「ID」); –

回答

2

YOu在JSONPost.SendHttpPost中收到異常,導致它返回null。這意味着當您嘗試對結果調用getJSONObject時,會得到空指針異常。它看起來像你試圖通過SendHttpPost函數在onCreate中執行HTTP請求。這是不允許的 - 你不能在UI線程上執行網絡IO,它會拋出異常。您需要在線程或AsyncTask中執行此操作。

+0

但是,當我改變線成字符串jsonObjRecv = JSONPost.SendHttpPost(URL,jsonObjSend);我可以接收它,但作爲String對象。 –

0

檢查此library了。它可以輕鬆幫助您獲取和解析正確的JSON響應。具體而言,對於JSON請求,它具有非常整齊的JsonHttpResponseHandler

0

HttpEntity entity = response.getEntity();'

這是返回您的任何數據?在使用響應對象之前,在那裏放置一個空檢查 還您sendhttppost

+0

是的,它會返回... –