我正在尝试从http://api.deezer.com/2.0/playlist/4341978中提取json内容,但我的日志会打印以下错误消息:
错误:org.json.JSONException:Value {“error”:{“message”:“必须使用活动访问令牌来查询有关当前用户的信息”,“类型”:“OAuthException”,“code”类型为org.json.JSONObject的类型:200}}无法转换为JSONArray
的Json
{
"id": 4341978,
"title": "Archive",
"duration": 2063,
"public": true,
"is_loved_track": false,
"collaborative": true,
"rating": 5,
"link": "http://www.deezer.com/playlist/4341978",
"picture": "http://api.deezer.com/2.0/playlist/4341978/image",
"creator": {
"id": 2529
},
"type": "playlist",
"tracks": {
"data": [
{
"id": 1152226,
"readable": true,
"title": "Darkroom",
"link": "http://www.deezer.com/track/1152226",
"duration": 271,
"preview": "http://cdn-preview-6.deezer.com/stream/6452dbcd46c90a70cb1147666ffd91ae-0.mp3",
"artist": {
"id": 1334,
"name": "Archive",
"link": "http://www.deezer.com/artist/1334"
},
"album": {
"id": 123427,
"title": "Londinium",
"cover": "http://api.deezer.com/2.0/album/123427/image"
},
"type": "track"
},
{
"id": 708335,
"readable": false,
"title": "Again",
"link": "http://www.deezer.com/track/708335",
"duration": 980,
"preview": "http://cdn-preview-d.deezer.com/stream/d8fe302b80c2be8a5fed4b8341f1286e-2.mp3",
"artist": {
"id": 1334,
"name": "Archive",
"link": "http://www.deezer.com/artist/1334"
},
"album": {
"id": 84795,
"title": "You All Look The Same To Me (open disc)",
"cover": "http://api.deezer.com/2.0/album/84795/image"
},
"type": "track"
},
{
"id": 778880,
"readable": false,
"title": "Fuck U",
"link": "http://www.deezer.com/track/778880",
"duration": 312,
"preview": "http://cdn-preview-5.deezer.com/stream/505ca648dde07ce1288de4a8762f36e6-2.mp3",
"artist": {
"id": 1334,
"name": "Archive",
"link": "http://www.deezer.com/artist/1334"
},
"album": {
"id": 91316,
"title": "Noise",
"cover": "http://api.deezer.com/2.0/album/91316/image"
},
"type": "track"
},
{
"id": 1965620,
"readable": true,
"title": "Stairway To Heaven",
"link": "http://www.deezer.com/track/1965620",
"duration": 500,
"preview": "http://cdn-preview-3.deezer.com/stream/3a1e4062efacdef98745cc2e05c84b66-1.mp3",
"artist": {
"id": 1548,
"name": "Dweezil Zappa",
"link": "http://www.deezer.com/artist/1548"
},
"album": {
"id": 199616,
"title": "Led Box - The Ultimate Led Zeppelin Tribute",
"cover": "http://api.deezer.com/2.0/album/199616/image"
},
"type": "track"
}
]
}
}
这是我的Asynctask
类MyAsyncTask扩展了AsyncTask {
private ProgressDialog progressDialog = new ProgressDialog(context);
InputStream inputStream = null;
String result = "";
protected void onPreExecute() {
progressDialog.setMessage("Downloading your data...");
progressDialog.show();
progressDialog.setOnCancelListener(new OnCancelListener() {
public void onCancel(DialogInterface arg0) {
MyAsyncTask.this.cancel(true);
}
});
}
@Override
protected Void doInBackground(String... params) {
String url_select = "http://api.deezer.com/2.0/playlist/4341978";
ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
try {
// Set up HTTP post
// HttpClient is more then less deprecated. Need to change to URLConnection
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url_select);
httpPost.setEntity(new UrlEncodedFormEntity(param));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
// Read content & Log
inputStream = httpEntity.getContent();
} catch (UnsupportedEncodingException e1) {
Log.e("UnsupportedEncodingException", e1.toString());
e1.printStackTrace();
} catch (ClientProtocolException e2) {
Log.e("ClientProtocolException", e2.toString());
e2.printStackTrace();
} catch (IllegalStateException e3) {
Log.e("IllegalStateException", e3.toString());
e3.printStackTrace();
} catch (IOException e4) {
Log.e("IOException", e4.toString());
e4.printStackTrace();
}
// Convert response to string using String Builder
try {
BufferedReader bReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"), 8);
StringBuilder sBuilder = new StringBuilder();
String line = null;
while ((line = bReader.readLine()) != null) {
sBuilder.append(line + "\n");
}
inputStream.close();
result = sBuilder.toString();
} catch (Exception e) {
Log.e("StringBuilding & BufferedReader", "Error converting result " + e.toString());
}
return null;
} // protected Void doInBackground(String... params)
protected void onPostExecute(Void v) {
//parse JSON data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0; i < jArray.length(); i++) {
JSONObject jObject = jArray.getJSONObject(i);
String name = jObject.getString("title");
String tab1_text = jObject.getString("type");
int active = jObject.getInt("tracks");
System.out.println("name: "+name+"\n");
System.out.println("tab1_text: "+tab1_text+"\n");
System.out.println("active: "+active+"\n");
} // End Loop
this.progressDialog.dismiss();
} catch (JSONException e) {
Log.e("JSONException", "Error: " + e.toString());
} // catch (JSONException e)
} // protected void onPostExecute(Void v)
} //类MyAsyncTask扩展了AsyncTask
我怎样才能解决这个错误
答案 0 :(得分:1)
应该是
JSONObject jobject = new JSONObject(result);
您没有json数组。它是一个json对象。
{
表示json对象节点。
[
表示json数组节点
"tracks": { // json object
"data": [ // json array
"title": "Darkroom", // json string
...
编辑:
JSSONObject jsono = new JSONObject(result);
JSONObject jb1 = jsono.getJSONObject("tracks");
JSONArray jar = jb1.getJSONArray("data");
for(int i=0;i<jar.length();i++)
{
JSONObject jb2= (JSONObject) jar.get(i);
String title = jb2.getString("title");
Log.i("...",title);
}