我要發佈數據到url
並得到Null pointer exception
我JSON
URL
包含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();
}
}
});
當您在response..Inside的JsonArray(「詳細信息‘)看到存在的JSON(’狀態」),這樣做PLZ正確解析 –
'Details'是一個jsonArray不是對象 –
Mounika有編輯Json解析代碼只是代表它 –