我正在使用android studio进行编程,并希望使用以下链接解析json: http://jsonplaceholder.typicode.com/posts 并将详细信息放在我创建的类中,该类命名详细信息并列出它们,但它会引发异常。我该怎么办? 主要活动:
public class MainActivity extends ActionBarActivity {
static ArrayList<Details> details= new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new myserver2().execute((String[]) null);
Adapter adptor=new Adapter(this,details);
ListView l= (ListView) findViewById(R.id.listview);
l.setAdapter(adptor);
Log.i("**(*si","si");
}
private class myserver2 extends AsyncTask<String,String,String> {
String s;
@Override
protected String doInBackground(String... params) {
HttpClient client = new DefaultHttpClient();
HttpGet hp = new HttpGet("http://jsonplaceholder.typicode.com/posts");
try
{
HttpResponse r = client.execute(hp);
HttpEntity ent = r.getEntity();
String s = EntityUtils.toString(ent);
JSONArray array = new JSONArray(s);
JSONObject obj = new JSONObject();
for(int i=0;i<array.length();i++){
Details d = null;
obj=array.getJSONObject(i);
d.setId(Integer.parseInt(obj.getString("id")));
d.setUserid(Integer.parseInt(obj.getString("userId")));
d.setBody(obj.getString("body"));
d.setTitle(obj.getString("title"));
details.add(d);
}
} catch (
IOException e
)
{
e.printStackTrace();
} catch (
JSONException e
)
{
e.printStackTrace();
}
return null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
详细说明:
public class Details {
int id=0,userid=0;
String body,title;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
} 和适配器:
public class Adapter extends ArrayAdapter<Details> {
Context c;
ArrayList<Details> details;
public Adapter(Context context, ArrayList<Details> objects) {
super(context, R.layout.item, objects);
c=context;
details=objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater= (LayoutInflater) c.getSystemService(c.LAYOUT_INFLATER_SERVICE);
View v=inflater.inflate(R.layout.item,parent,false);
TextView body= (TextView) v.findViewById(R.id.body);
TextView title= (TextView) v.findViewById(R.id.title);
String body2= details.get(position).getBody();
String title2= details.get(position).getTitle();
body.setText(body2);
title.setText(title2);
return v;
}
}
但它引发了一个例外:
Caused by: java.lang.NullPointerException
at com.etsiamak.practice5.MainActivity$myserver2.doInBackground(MainActivity.java:56)
at com.etsiamak.practice5.MainActivity$myserver2.doInBackground(MainActivity.java:37)
这两行:
d.setId(Integer.parseInt(obj.getString("id")));
和
private class myserver2 extends AsyncTask<String,String,String> {
答案 0 :(得分:0)
Yous应该检查:
d.setId(Integer.parseInt(obj.getString("id")));
如果您调试考虑所有这些,最有可能的是您可以找到错误或者至少向我们提供有关它的更多信息。