2016-03-15 72 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();