[英]Android JSON null pointer Exception while parsing
I am beginner to android .I am making one application related to webservice.I am getting json by hitting url and I want to show in listview .I tried so many ways but no result.I got json exceptions.please help me Here is my json: 我是android的初学者。我正在制作一个与web服务相关的应用程序。我通过击url获取json,并且想在listview中显示。我尝试了很多方法但没有结果。我得到了json异常。请帮助我这是我的JSON:
[
[
{
"id":"9637",
"country":"Australia",
"time":"14:00",
"type":"country",
"status":"good"
},
{
"id":"9638",
"country":"india",
"time":"16:00",
"type":"country",
"status":"good"
}
]
]
code:
class Response extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
jsonResponse = EntityUtils.toString(httpEntity);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result) {
Toast.makeText(MainActivity.this,jsonResponse,Toast.LENGTH_SHORT).show();
try {
JSONArray jsonArray=new JSONArray(jsonResponse);
for(int i=0;i<jsonArray.length();i++)
{
Toast.makeText(MainActivity.this,""+jsonArray.length(),Toast.LENGTH_SHORT).show();
}
// JSONArray jsonArray=jsonObject.get
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
super.onPostExecute(result);
}
I think this code is correct: 我认为这段代码是正确的:
try {
JSONArray json=new JSONArray(jsonResponse);
JSONArray jsonArray=json.getJSONArray(0);
for(int i=0;i<jsonArray.length();i++)
{
Toast.makeText(MainActivity.this,""+jsonArray.length(),Toast.LENGTH_SHORT).show();
}
// JSONArray jsonArray=jsonObject.get
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
because your JSON Response is an array of arrays. 因为您的JSON响应是一个数组数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.