当我在本地主机中尝试时,它可以工作。这是我的本地主机提供的JSON。
是这个网址http://api.androidhive.info/contacts/中的错误
[{
"id": "1",
"first_name": "man",
"last_name": "woman",
"username": "man",
"password": "4f70432e6369
70de9929bcc6f1b72412",
"email": "man@gmail.com",
"url": "http:\/\/localhost\/adchara1\/"
}, {
"id": "6",
"first_name": "first",
"last_name": "Last
Name",
"username": "user",
"password": "1a1dc91c907325c69271ddf0c944bc72",
"email": "0",
"url": "ht
tp:\/\/api.androidhive.info\/contacts\/"
}, {
"id": "7",
"first_name": "1",
"last_name": "2",
"username": "us45",
"password": "33d8f54e33896a5722
7b18642979e558",
"email": "first@gmail.com",
"url": "http:\/\/ugirusgiarto.wordpress.com\/2011\
/10\/27\/json-php-mysql-with-asynctask-progressdialog\/"
}, {
"id": "9",
"first_name": "First Name",
"last_name": "Last
Name",
"username": "Username",
"password": "dc647eb65e6711e155375218212b3964",
"email": "woman@gm
ail.com",
"url": "http:\/\/mobile.cs.fsu.edu\/parse-json-objects-in-asynctask\/"
}]
X
主要活动
public class MainActivity extends Activity {
TextView text_1, text_2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new task().execute();
}
class task extends AsyncTask<String, String, Void> {
private ProgressDialog progressDialog = new ProgressDialog(
MainActivity.this);
InputStream is = null;
String result = "";
protected void onPreExecute() {
progressDialog.setMessage("Download data...");
progressDialog.show();
progressDialog.setOnCancelListener(new OnCancelListener() {
public void onCancel(DialogInterface arg0) {
task.this.cancel(true);
}
});
}
@Override
protected Void doInBackground(String... params) {
// String url_select = "http://192.168.10.111/adchara1/";
String url_select = "http://api.androidhive.info/contacts/";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url_select);
ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
try {
httpPost.setEntity(new UrlEncodedFormEntity(param));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
// read content
is = httpEntity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = "";
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
// TODO: handle exception
Log.e("log_tag", "Error converting result " + e.toString());
}
return null;
}
protected void onPostExecute(Void v) {
// ambil data dari Json database
try {
JSONArray Jarray = new JSONArray(result);
for (int i = 0; i < Jarray.length(); i++) {
JSONObject Jasonobject = null;
text_1 = (TextView) findViewById(R.id.txt1);
Jasonobject = Jarray.getJSONObject(i);
// get an output on the screen
String id = Jasonobject.getString("id");
String name = Jasonobject.getString("name");
String email = Jasonobject.getString("email");
String address = Jasonobject.getString("address");
String gender = Jasonobject.getString("gender");
text_1.append("\n" + id + "\t\t" + name + "\t\t\t"
+ email + "\t\t\t\t" + address + "\t\t\t\t" + gender
+ "\t\t\t\t" + "\n");
}
this.progressDialog.dismiss();
} catch (Exception e) {
// TODO: handle exception
Log.e("log_tag", "Error parsing data " + e.toString());
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
错误
Error parsing data org.json.JSONException: Value {
"contacts": [{
"id": "c200",
"gender": "male",
"phone": {
"office": "00 000000",
"home": "00 000000",
"mobile": "+91 0000000000"
},
"address": "xx-xx-xxxx,x - street, x - country",
"email": "ravi@gmail.com",
"name": "Ravi Tamada"
}, {
"id": "c201",
"gender": "male",
"phone": {
"office": "00 000000",
"home": "00 000000",
"mobile": "+91 0000000000"
},
"address": "xx-xx-xxxx,x - street, x - country",
"email": "johnny_depp@gmail.com",
"name": "Johnny Depp"
}, {
"id": "c202",
"gender": "male",
"phone": {
"office": "00 000000",
"home": "00 000000",
"mobile": "+91 0000000000"
},
"address": "xx-xx-xxxx,x - street, x - country",
"email": "leonardo_dicaprio@gmail.com",
"name": "Leonardo Dicaprio"
}, {
"id": "c203",
"gender": "male",
"phone": {
"office": "00 000000",
"home": "00 000000",
"mobile": "+91 0000000000"
},
"address": "xx-xx-xxxx,x - street, x - country",
"email": "john_wayne@gmail.com",
"name": "John Wayne"
}, {
"id": "c204",
"gender": "female",
"phone": {
"office": "00 000000",
"home": "00 000000",
"mobile": "+91 0000000000"
},
"address": "xx-xx-xxxx,x - street, x - country",
"email": "angelina_jolie@gmail.com",
"name": "Angelina Jolie"
}, {
"id": "c205",
"gender": "female",
"phone": {
"office": "00 000000",
"home": "00 000000",
"mobile": "+91 0000000000"
},
"address": "xx-xx-xxxx,x - street, x - country",
"email": "dido@gmail.com",
"name": "Dido"
}, {
"id": "c206",
"gender": "female",
"phone": {
"office": "00 000000",
"home": "00 000000",
"mobile": "+91 0000000000"
},
"address": "xx-xx-xxxx,x - street, x - country",
"email": "adele@gmail.com",
"name": "Adele"
}, {
"id": "c207",
"gender": "male",
"phone": {
"office": "00 000000",
"home": "00 000000",
"mobile": "+91 0000000000"
},
"address": "xx-xx-xxxx,x - street, x - country",
"email": "hugh_jackman@gmail.com",
"name": "Hugh Jackman"
}, {
"id": "c208",
"gender": "male",
"phone": {
"office": "00 000000",
"home": "00 000000",
"mobile": "+91 0000000000"
},
"address": "xx-xx-xxxx,x - street, x - country",
"email": "will_smith@gmail.com",
"name": "Will Smith"
}, {
"id": "c209",
"gender": "male",
"phone": {
"office": "00 000000",
"home": "00 000000",
"mobile": "+91 0000000000"
},
"address": "xx-xx-xxxx,x - street, x - country",
"email": "clint_eastwood@gmail.com",
"name": "Clint Eastwood"
}, {
"id": "c2010",
"gender": "male",
"phone": {
"office": "00 000000",
"home": "00 000000",
"mobile": "+91 0000000000"
},
"address": "xx-xx-xxxx,x - street, x - country",
"email": "barack_obama@gmail.com",
"name": "Barack Obama"
}, {
"id": "c2011",
"gender": "female",
"phone": {
"office": "00 000000",
"home": "00 000000",
"mobile": "+91 0000000000"
},
"address": "xx-xx-xxxx,x - street, x - country",
"email": "kate_winslet@gmail.com",
"name": "Kate Winslet"
}, {
"id": "c2012",
"gender": "male",
"phone": {
"office": "00 000000",
"home": "00 000000",
"mobile": "+91 0000000000"
},
"address": "xx-xx-xxxx,x - street, x - country",
"email": "eminem@gmail.com",
"name": "Eminem"
}]
}
of type org.json.JSONObject cannot be converted to JSONArray
您可以尝试以下方法:
JSONObject object = new JSONObject(result);
JSONArray Jarray = object.getJSONArray("contacts");
for (int i = 0; i < Jarray.length(); i++)
{
JSONObject Jasonobject = Jarray.getJSONObject(i);
}
问题内容: 在json中工作时出现异常。我的JSONPresr类如下 我正在通过这些代码获取数据 但是我非常惊讶地在这一行中获得异常 我的股权跟踪消息为 我尝试了许多链接,但不幸的是我无法获得结果。请任何人帮助我解决这个问题。在此先感谢所有人 问题答案: 从错误中可以明显看出,您正在尝试将Json Object转换为Json数组。那不应该。 这是读取您的JSON响应的代码。 上面的代码用于JSON
问题内容: 当我运行以下代码时… …在运行时,我在日志输出中看到以下内容: 我不明白为什么。 问题答案: 您输入了错误的类。更改 至
我点击API,得到的响应是 我试图获取用户名的逻辑。 我做错了什么?
这是我得到错误的代码 这是我的AnalyticsApplication类 这是logcat上的错误 03-09 18:46:11.070 32602-32602/auc。games2.multigame1 E/AndroidRuntime:致命异常:主java。lang.ClassCastException:android。应用程序。无法将应用程序转换为auc。游戏2.分析。auc的分析应用。ga
我不知道我在这方面哪里出了问题,我正在尝试在recyclerView中列出食谱的成分,我只是无法让onBindViewHolder工作,不管我尝试了什么。下面是适配器代码以及xml配方。班本质上,我需要向recyclerView显示Recipe类的结果。 食谱班 适配器 完整logcat RecipeList模型公共类RecipeList{ 更新的错误 尝试在空对象引用上调用虚方法“空androi