我是newbaby android并做一些关于Volley的练习。我写了示例代码:
RequestQueue rq = Volley.newRequestQueue(this);
String urlStr="https://api.github.com/users/mralexgray/repos";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
com.android.volley.Request.Method.GET,urlStr, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("Response", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, error.toString(),
Toast.LENGTH_LONG).show();
Log.d("Error!!!!", error.toString());
}
});
rq.add(jsonObjectRequest);
但是程序抛出异常:
com.android.volley.ParseError: org.json.JSONException: Value [{"has_issues":true,"teams_url":"https:\/\/api.github.com\/repos\/mralexgray\/-REPONAME\/teams","releases_url":"https:\/\/api.github.com\/repos\/mralexgray\/-REPONAME\/releases{\/id}","compare_url":"https:\/\/api.github.com\/repos\/mralexgray\/-REPONAME\/compare\/{base}...{head}","keys_url":"https:\/\/api.github.com\/repos\/mralexgray\/-REPONAME\/keys{\/key_id}","milestones_url":"https:\/\/api.github.com\/repos\/mralexgray\/-REPONAME\/milestones{\/number}","description":null,"has_wiki":true,"events_url":"https:\/\/api.github.com\/repos\/mralexgray\/-REPONAME\/events","archive_url":"https:\/\/api.github.com\/repos\/mralexgray\/-REPONAME\/{archive_format}{\/ref}","subscribers_url":"https:\/\/api.github.com\/repos\/mralexgray\/-REPONAME\/subscribers","contributors_url":"https:\/\/api.github.com\/repos\/mralexgray\/-REPONAME\/contributors","pushed_at":"2012-10-06T16:37:39Z","fork":false,"svn_url":"https:\/\/github.com\/mralexgray\/-REPONAME","collaborators_url":"https:\/\/api.github.com\/repos\/mralexgray\/-REPONAME\/collaborators{\/collaborator}","subscription_url":"https:\/\/api.github.com\/repos\/mralexgray\/-REPONAME\/subscription","clone_url":"https:\/\/github.com\/mralexgray\/-REPONAME.git","trees_url":"https:\/\/api.github.com\/repos\/mralexgray\/-REPONAME\/git\/trees{\/sha}","homepage":null,"url":"https:\/\/api.github.com\/repos\/mralexgray\/-REPONAME","size":76,"notifications_url":"https:\/\/api.github.com\/repos\/mralexgray\/-REPONAME\/notifications{?since,all,participating}","updated_at":"2013-01-12T13:39:30Z","branches_url":"https:\/\/api.github.com\/repos\/mralexgray\/-REPONAME\/branches{\/branch}","owner":{"received_events_url":"https:\/\/api.github.com\/users\/mralexgray\/received_events","organizations_url":"https:\/\/api.github.com\/users\/mralexgray\/orgs","avatar_url":"https:\/\/0.gravatar.com\/avatar\/50e7ed4eb2e7af000ea7d161748958f1?d=https%3A%2F%2Fidenticons.github.com%2F27bbb5393e06a225e4ecf9c4ce6feabf.png&r=x","gravatar_id":"50e7ed4eb2e7af000ea7d161748958f1","gists_url":"https:\/\/api.github.com\/users\/mralexgray\/gists{\/gist_id}","starred_url":"https:\/\/api.github.com\/users\/mralexgray\/starred{\/owner}{\/repo}","site_admin":false,"type":"User","url":"https:\/\/api.github.com\/users\/mralexgray","html_url":"https:\/\/github.com\/mralexgray","id":262517,"following_url":"https:\/\/api.github.com\/users\/mralexgray\/following{\/other_user}","events_url":"https:\/\/api.github.com\/users\/mralexgray\/events{\/privacy}","login":"mralexgray","subscriptions_url":"https:\/\/api.github.com\/users\/mralexgray\/subscriptions","repos_url":"https:\/\/api.github.com\/users\/mralexgray\/repos","followers_url":"https:\/\/api.github.com\/users\/mralexgray\/followers"},"issue_events_url":"https:\/\/api.github.com\/repos\/mralexgray\/-REPONAME\/issues\/events{\/number}","language":null,"forks_count":0,"contents_url":"https:\/\/api.github.com\/repos\/mralexgray\/-REPONAME\/contents\/{+path}","watchers_count":0,"blobs_url":"https:\/\/api.github.com\/repos\/mralexgray\/-REPONAME\/git\/blobs{\/sha}","commits_url":"https:\/\/api.github.com\/repos\/mralexgray\/-REPONAME\/commits{\/sha}","has_downloads":true,"git_commits_url":"https:\/\/api.github.com\/repos\/mralexgray\/-REPONAME\/git\/commits{\/sha}","private":false,"open_issues":0,"id":6104546,"downloads_url":"https:\/\/api.github.com\/repos\/mralexgray\/-REPONAME\/downloads","mirror_url":null,"comments_url":"https:\/\/api.github.com\/repos\/mralexgray\/-REPONAME\/comments{\/number}","name":"-REPONAME","created_at":"2012-10-06T16:37:39Z","stargazers_count":0,"assignees_url":"https:\/\/api.github.com\/repos\/mralexgray\/-REPONAME\/assignees{\/user}","pulls_url":"https:\/\/api.github.com\/repos\/mralexgray\/-REPONAME\/pulls{\/number}","watchers":0,"stargazers_url":"https:\/\/api.github.com\/repos\/mralexgray\/-REPONAME\/stargazers","hooks_url":"https:\/\/api.github.com\/repos\/mralexgray\/-REPONAME\/hooks","languages_url":"https:\/\/api.github.com\/repos\/mralexgray\/-REPONAME\/la
如何解决此错误?
答案 0 :(得分:1)
响应是Json数组而不是json对象因此修改Volley请求方法如下
RequestQueue rq = Volley.newRequestQueue(this);
String urlStr = "https://api.github.com/users/mralexgray/repos";
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(urlStr,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
// TODO Auto-generated method stub
Log.d("Response", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, error.toString(),
Toast.LENGTH_LONG).show();
Log.d("Error!!!!", error.toString());
}
});
rq.add(jsonArrayRequest);
这会对你有所帮助