2013-12-09 73 views
0

我是新手安卓,並對Volley做了一些練習。我寫的示例代碼:安卓排球JsonPars異常

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 

我如何解決這個錯誤?

+1

你貼是JsonArray,不是一個JSONObject的JSON的部分,對於初學者。此外,它看起來像混合了JSONObject和JsonObject類,這可能會導致問題,因爲它們不是同一件事。我無法確切地告訴你錯誤來自哪裏,因爲我看到了幾處可能失敗的地方,並且你沒有發佈完整跟蹤,但如果響應是一個數組並且確保不要混合組織,請嘗試請求一個JsonArray。 json和com.google.gson類。 – Turnsole

回答

1

的響應是一個JSON數組不是JSON對象,所以修改凌空請求方法如下

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); 

這將幫助你