分析时出现Android JSON空指针异常
我是android初学者。我正在制作一个与webservice相关的应用程序。我通过点击url获得json,我想在listview中显示。我尝试了很多方法,但没有结果。我得到了json异常。请帮助我 这是我的json:分析时出现Android JSON空指针异常,android,json,nullpointerexception,Android,Json,Nullpointerexception,我是android初学者。我正在制作一个与webservice相关的应用程序。我通过点击url获得json,我想在listview中显示。我尝试了很多方法,但没有结果。我得到了json异常。请帮助我 这是我的json: [ [ { "id":"9637", "country":"Australia", "time":"14:00", "type":"country", "status":"good" }, {
[
[
{
"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);
}
[
[
{
“id”:“9637”,
“国家”:“澳大利亚”,
“时间”:“14:00”,
“类型”:“国家”,
“状态”:“良好”
},
{
“id”:“9638”,
“国家”:“印度”,
“时间”:“16:00”,
“类型”:“国家”,
“状态”:“良好”
}
]
]
代码:
类响应扩展了异步任务{
@凌驾
受保护的Void doInBackground(Void…参数){
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
试一试{
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
jsonResponse=EntityUtils.toString(httpEntity);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回null;
}
受保护的void onPostExecute(void结果){
makeText(MainActivity.this、jsonResponse、Toast.LENGTH_SHORT.show();
试一试{
JSONArray JSONArray=新的JSONArray(jsonResponse);
对于(int i=0;i我认为该代码是正确的:
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();
}
试试看{
JSONArray json=新的JSONArray(jsonResponse);
JSONArray JSONArray=json.getJSONArray(0);
对于(int i=0;i代码在这里,异常stacktrace在哪里?您缺少一个引号@“状态”:“好”
发布关于处理json的代码string@shayan你确定状态
是“状态”
是因为两者都有错误吗???@MDMalik不,我不确定,我只是编辑了它,因为我认为这是写错误,