2013-02-01 3 views
2

J'essaie de publier l'objet json, puis essayez de recevoir l'objet json. Grâce à sniffer il montre ce poste et obtenir des travaux réussis. Mais l'objet récepteur est toujours nul. Je pense que c'est à cause de mon insouciance, alors j'espère que vous m'aiderez.NullPointerException lors du retour de l'objet 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(); 
} 

et voici une partie de l'activité où je continue à obtenir l'erreur NullPointerException à la ligne 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(); 
    } 

Quand j'ai changé de type jsonObjRecv à cordes, il a commencé à travailler, mais je dois le type JSON pour elle .

Erreur:

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 

Merci pour votre réponse.

+0

passé ur réponse JSON – MuraliGanesan

+0

pouvez-vous indiquer ce qui est sur OtherActivity.java, ligne 47 –

+0

@DavidM, 47 ligne est: jsonObjRecv.getJSONObject ("id"); –

Répondre

2

Vous obtenez une exception dans JSONPost.SendHttpPost, ce qui provoque l'annulation de la valeur null. Cela signifie que vous obtenez une exception null pointeur lorsque vous essayez d'appeler getJSONObject sur le résultat. Il semble que vous essayez de faire une requête HTTP dans onCreate via la fonction SendHttpPost. Ceci n'est pas autorisé - vous ne pouvez pas faire d'E/S réseau sur le thread de l'interface utilisateur, il déclenche une exception. Vous devez le faire dans un thread ou une tâche asynchrone.

+0

Mais, quand je change la ligne en String jsonObjRecv = JSONPost.SendHttpPost (URL, jsonObjSend); Je peux le recevoir, mais en tant qu'objet String. –

0

Cochez cette case library. Il vous aidera facilement à obtenir et à analyser les réponses JSON correctes. Plus précisément, il a un très bon JsonHttpResponseHandler pour les demandes JSON.

0

HttpEntity entity = réponse.getEntity(); '

est-ce que cela retourne vos données? Placez une vérification null là avant d'utiliser l'objet de réponse. aussi vous retourner null pour une exception dans sendhttppost

+0

Oui, ça revient ... –

Questions connexes