Java 作为json结构的Android联系人列表无法获取
我想获取android联系人详细信息,如:Java 作为json结构的Android联系人列表无法获取,java,android,arrays,json,Java,Android,Arrays,Json,我想获取android联系人详细信息,如: [ { "id" : "1", "name" : "name1", "mobile":"1234567891" }, { "id" : "2", "name" : "name2", "mobile":"2222222222" } ] 所以我有这个代码 Contact.java public class Contact {
[
{
"id" : "1",
"name" : "name1",
"mobile":"1234567891"
},
{
"id" : "2",
"name" : "name2",
"mobile":"2222222222"
}
]
所以我有这个代码
Contact.java
public class Contact {
private String id;
private String name;
private String mobile;
//setters & getters
//constructors
}
执行后台任务,然后显示列表的AsyncTask
private class MyAsync extends AsyncTask<String, Void, String> {
@SuppressWarnings("unused")
@Override
protected String doInBackground(String... params) {
String serverResponse = "";
try {
serverResponse =contactDetail.fetchContacts1();
Contact contact1= new Contact();
contact1 = new Gson().fromJson(serverResponse, Contact.class);
Log.i("serverResponse", serverResponse);
} catch (Exception e) {
e.printStackTrace();
}
return serverResponse;
}
@Override
protected void onPostExecute(String serverResponse) {
outputText.setText(serverResponse);
}
}
但是,在显示屏上,我只能看到一个电话号码的详细信息,而不是所有的电话号码的详细信息
[{ "id" : "1","name" : "name1", "mobile":"1234567891" } ]
我的代码有什么问题?您在'id'行中遗漏了一个前导的
“
:“2”`请检查@Martin Zabel上的第一个json是否有效,我已更正。.这只是日志输出值[{“id”:“1”,“name”:“name1”,“mobile”:“1234567891”}]您遗漏了“,@Dixit Patel,这是输出/logcat值..我试图表达我想要的结构以及得到的结果。。
[{
"id" : "1",
"name" : "name1",
"mobile":"1234567891"
}]
...
.....
[{
"id" : "2",
"name" : "name2",
"mobile":"2222222222"
}]
[{ "id" : "1","name" : "name1", "mobile":"1234567891" } ]