Android : org json json异常无值
Android : org json jsonexception no value for
我用这个 json url :
https://www.theaudiodb.com/api/v1/json/1/search.php?s=coldplay
{
"artists": [
{
"idArtist": "111239",
"strArtist": "Coldplay",
"strArtistStripped": null,
"strArtistAlternate": "",
"strLabel": "Parlophone",
"idLabel": "45114",
"intFormedYear": "1996",
"intBornYear": "1996",
"intDiedYear": null,
"strDisbanded": null,
"strStyle": "Rock/Pop",
"strGenre": "Alternative Rock",
etc
}
这是我的 android java 代码:
void syncToUrl() {
AsyncHttpClient client = new AsyncHttpClient();
String url = "https://www.theaudiodb.com/api/v1/json/1/search.php?s=coldplay";
client.get(url, new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
parseAndShow(responseString);
}
});
}
void parseAndShow(String responce) {
try {
JSONObject artistBio = new JSONObject(responce);
artistNickname.setText(artistBio.getString("strArtist"));
} catch (Exception e) {
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
Error.setText(e.toString());
}
}
但我仍然收到此错误:
org.json.jsonexception strArtist 没有价值。
strArtist 具有此值 "Coldplay",但为什么会出现此错误?
可以得到strArtist
的值如下:
JSONObject artistBio = new JSONObject(responce);
JSONArray artists = artistBio.getJSONArray("artists");
for (int i=0; i < artists.length(); i++) {
JSONObject object = artists.getJSONObject(i);
String name = actor.getString("strArtist");
artistNickname.setText(artistBio.getString("strArtist"));
}
编辑
由于你的json数组里面只有一个对象,你可以跳过for循环的实现,直接使用如下:
JSONObject object = artists.getJSONObject(0);
String name = actor.getString("strArtist");
artistNickname.setText(artistBio.getString("strArtist"));
我用这个 json url : https://www.theaudiodb.com/api/v1/json/1/search.php?s=coldplay
{
"artists": [
{
"idArtist": "111239",
"strArtist": "Coldplay",
"strArtistStripped": null,
"strArtistAlternate": "",
"strLabel": "Parlophone",
"idLabel": "45114",
"intFormedYear": "1996",
"intBornYear": "1996",
"intDiedYear": null,
"strDisbanded": null,
"strStyle": "Rock/Pop",
"strGenre": "Alternative Rock",
etc
}
这是我的 android java 代码:
void syncToUrl() {
AsyncHttpClient client = new AsyncHttpClient();
String url = "https://www.theaudiodb.com/api/v1/json/1/search.php?s=coldplay";
client.get(url, new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
parseAndShow(responseString);
}
});
}
void parseAndShow(String responce) {
try {
JSONObject artistBio = new JSONObject(responce);
artistNickname.setText(artistBio.getString("strArtist"));
} catch (Exception e) {
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
Error.setText(e.toString());
}
}
但我仍然收到此错误: org.json.jsonexception strArtist 没有价值。
strArtist 具有此值 "Coldplay",但为什么会出现此错误?
可以得到strArtist
的值如下:
JSONObject artistBio = new JSONObject(responce);
JSONArray artists = artistBio.getJSONArray("artists");
for (int i=0; i < artists.length(); i++) {
JSONObject object = artists.getJSONObject(i);
String name = actor.getString("strArtist");
artistNickname.setText(artistBio.getString("strArtist"));
}
编辑
由于你的json数组里面只有一个对象,你可以跳过for循环的实现,直接使用如下:
JSONObject object = artists.getJSONObject(0);
String name = actor.getString("strArtist");
artistNickname.setText(artistBio.getString("strArtist"));