2016-03-15 104 views
-1

我要發佈數據url並得到Null pointer exceptionJSONURL包含JSON對象顯示空指針異常

{ 
    "Details": 
    [ 
     { 
     "Status":"NO UPDATES" 
     } 
    ] 
} 

我得到了錯誤的行:

String status = object.getString("Status").trim(); //error Line 

完整代碼:

btnPost = (Button)findViewById(R.id.btnPost); 

    btnPost.setOnClickListener(this); 
    btnPost.setOnClickListener(new OnClickListener() 
    { 
     @SuppressWarnings("null") 
     @Override 
     public void onClick(View arg0) 
     { 
      try 
      { 
       String postReceiverUrl = "http://"; 
       Log.v(TAG, "postURL: " + postReceiverUrl); 
       // HttpClient 
       @SuppressWarnings("resource") 
       HttpClient httpClient = new DefaultHttpClient(); 
       // post header 
       HttpPost httpPost = new HttpPost(postReceiverUrl); 

       jsonobject.put("IDNo", IDNo.getText().toString()); 
       jsonobject.put("Position", Position.getText().toString()); 
       jsonobject.put("Data", Data.getText().toString()); 

       HttpResponse response = httpClient.execute(httpPost); 

       String jsonResult = inputStreamToString(response.getEntity().getContent()).toString(); 

       Log.v("jsonResult",jsonResult); 

       JSONObject object = new JSONObject(jsonResult); 

       String status = object.getString("Status").trim(); 

       Toast.makeText(getBaseContext(), "Please wait...",100).show(); 

       if(status.toString().equals("SUCCESS")) 
       { 
        Intent i = new Intent(LoginPage.this,MainActivity.class); 
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        startActivity(i); 
       } 
       if(status.toString().equals("FAILED")) 
       { 
        Toast.makeText(getBaseContext(), "Wrong Credentials",100).show(); 
       } 

       else 
       { 
        Toast.makeText(getBaseContext(), "Details Inserted",100).show(); 
       } 

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

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


     } 

    }); 
+1

當您在response..Inside的JsonArray(「詳細信息‘)看到存在的JSON(’狀態」),這樣做PLZ正確解析 –

+0

'Details'是一個jsonArray不是對象 –

+0

Mounika有編輯Json解析代碼只是代表它 –

回答

0

你的代碼應該是這樣的

JSONObject object = new JSONObject(jsonResult); 

      JSONOArray details = object.getJSONArray("Details"); 

      for (int i = 0; i < details.length(); i++) { 
      JSONObject c = details.getJSONObject(i); 

      String status = c.getString("Status"); 
      } 
      Toast.makeText(getBaseContext(), "Please wait...",100).show(); 
+0

Thanku for your re如果iam在 上收到新錯誤,JSONObject對象= new JSONObject(jsonResult); 錯誤是org.json.JSONException:值 Mounika

+0

是你的整個JSON @Mounika – curiousMind

+0

是@curiousMind – Mounika

0

Modift String status = object.getString("Status").trim();

String status = object.get("Details").getAsJsonArray()[0].getString("Status").trim(); 
0

您正在使用getString("Status")可能返回null如果密鑰不可在JSON。我建議你使用optString("Status")它將返回空白字符串,如果密鑰不可用。

JSONObject jsonObject = new JSONObject(stringJSON); 
JSONArray jsonArray = jsonObject.optJSONArray("Details"); 
if(jsonArray != null){ 
    for(int i = 0; i < jsonArray.length(); i++){ 
    JSONObject jObject = jsonArray.optJSONObject(i); 
    if(jObject != null){ 
     String strStatus = jObject.optString("Status"); 
    } 
    } 
} 
0

試試這個代碼

JSONObject object = new JSONObject(jsonResult); 
JSONArray array=object.getJsonarray("Details"); 
for(int i=0;i<array.length();i++) 
    { 

    JSONObject innerObject=array.getJsonObject(i); 
    String s= innerObject.getString("Status"); 

    } 
1

執行JSON爲正確解析,你看你的回答: -

{ "Details":[{"Status":"NO UPDATES"}]} 

所以首先儘量讓JSONObject的對象比在JSONArray之後,請看下面的例子: -

JSONObject jsonObject = new JSONObject(jsonResult); 
JSONArray detailsArray = jsonObject.getJSONArray("Details"); 

String status = dataArray.getJSONObject(0).getString("status"); 

Toast.makeText(getBaseContext(), "Please wait...",100+status).show();