2017-09-23 32 views
0

我正在開發一個應用程序,我想要訪問Web服務並獲取數據。我正在使用Google Volley來訪問網絡服務。我正在做的一切正常,但無法使用GSON解析JSON。我無法理解我在哪裏做錯了。如何解析JSON在Android中使用GSON

Here is the URL of Web Services

下面是我在做什麼

RequestQueue requestQueue = Volley.newRequestQueue(this); 
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
      Request.Method.GET, 
      "https://api.github.com/repos/crashlytics/secureudid/issues", 
      null, 
      new MyListner(), 
      new MyErrorListner() 
    ); 

    requestQueue.add(jsonObjectRequest); 

} 

class MyListner implements Response.Listener<JSONObject> 
{ 
    @Override 
    public void onResponse(JSONObject response) { 

     Gson gson = new Gson(); 
     Results results = gson.fromJson(response.toString(), Results.class); 

     for(Test t : results.getmResults()) 
     { 
      Log.e("Tag", t.toString()); 
     } 


    } 
} 

class MyErrorListner implements Response.ErrorListener 
{ 
    @Override 
    public void onErrorResponse(VolleyError error) { 
     Log.e("Error", error.getMessage()); 
    } 
} 

其他類給出如下

public class Results { 


private ArrayList<Test> mResults; 

public void setResults(ArrayList<Test> results) 
{ 
    mResults = results; 
} 

public ArrayList<Test> getmResults() 
{ 
    return mResults; 
} 
} 

public class Test { 

@SerializedName("title") 
private String mTitle; 

@SerializedName("user") 
private User mUser; 

@SerializedName("created_at") 
private String mCreatedAt; 

@SerializedName("body") 
private String mBody; 

@SerializedName("updated_at") 
private String mUpdatedAt; 

public String getmBody() { 
    return mBody; 
} 

public void setmBody(String mBody) { 
    this.mBody = mBody; 
} 

public String getmUpdatedAt() { 
    return mUpdatedAt; 
} 

public void setmUpdatedAt(String mUpdatedAt) { 
    this.mUpdatedAt = mUpdatedAt; 
} 

public String getmCreatedAt() { 
    return mCreatedAt; 
} 

public void setmCreatedAt(String mCreatedAt) { 
    this.mCreatedAt = mCreatedAt; 
} 

public User getmUser() { 
    return mUser; 
} 

public void setmUser(User mUser) { 
    this.mUser = mUser; 
} 

public String getmTitle() { 
    return mTitle; 
} 

public void setmTitle(String mTitle) { 
    this.mTitle = mTitle; 
} 

@Override 
public String toString() { 
    return mTitle + " " + mBody + " " + mCreatedAt + " " + mUpdatedAt + " " + mUser.getmLogin() + " "; 
} 
} 


public class User { 

@SerializedName("login") 
private String mLogin; 

public String getmLogin() { 
    return mLogin; 
} 

public void setmLogin(String mLogin) { 
    this.mLogin = mLogin; 
} 
} 

這裏是我得到的錯誤

org.json.JSONException: Value [{"url":"https:\/\/api.github.com\/repos\/crashlytics\/secureudid\/issues\/28","repository_url":"https:\/\/api.github.com\/repos\/crashlytics\/secureudid","labels_url":"https:\/\/api.github.com\/repos\/crashlytics\/secureudid\/issues\/28\/labels{\/name}","comments_url":"https:\/\/api.github.com\/repos\/crashlytics\/secureudid\/issues\/28\/comments","events_url":"https:\/\/api.github.com\/repos\/crashlytics\/secureudid\/issues\/28\/events","html_url":"https:\/\/github.com\/crashlytics\/secureudid\/pull\/28","id":222258999,"number":28,"title":"Fix broken headings in Markdown files","user":{"login":"bryant1410","id":3905501,"avatar_url":"https:\/\/avatars3.githubusercontent.com\/u\/3905501?v=4","gravatar_id":"","url":"https:\/\/api.github.com\/users\/bryant1410","html_url":"https:\/\/github.com\/bryant1410","followers_url":"https:\/\/api.github.com\/users\/bryant1410\/followers","following_url":"https:\/\/api.github.com\/users\/bryant1410\/following{\/other_user}","gists_url":"https:\/\/api.github.com\/users\/bryant1410\/gists{\/gist_id}","starred_url":"https:\/\/api.github.com\/users\/bryant1410\/starred{\/owner}{\/repo}","subscriptions_url":"https:\/\/api.github.com\/users\/bryant1410\/subscriptions","organizations_url":"https:\/\/api.github.com\/users\/bryant1410\/orgs","repos_url":"https:\/\/api.github.com\/users\/bryant1410\/repos","events_url":"https:\/\/api.github.com\/users\/bryant1410\/events{\/privacy}","received_events_url":"https:\/\/api.github.com\/users\/bryant1410\/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2017-04-17T23:26:47Z","updated_at":"2017-04-17T23:26:47Z","closed_at":null,"author_association":"NONE","pull_request":{"url":"https:\/\/api.github.com\/repos\/crashlytics\/secureudid\/pulls\/28","html_url":"https:\/\/github.com\/crashlytics\/secureudid\/pull\/28","diff_url":"https:\/\/github.com\/crashlytics\/secureudid\/pull\/28.diff","patch_url":"https:\/\/github.com\/crashlytics\/secureudid\/pull\/28.patch"},"body":"GitHub changed the way Markdown headings are parsed, so this change fixes it.\n\nSee [bryant1410\/readmesfix](https:\/\/github.com\/bryant1410\/readmesfix) for more information.\n\nTackles bryant1410\/readmesfix#1\n"},{"url":"https:\/\/api.github.com\/repos\/crashlytics\/secureudid\/issues\/13","repository_url":"https:\/\/api.github.com\/repos\/crashlytics\/secureudid","labels_url":"https:\/\/api.github.com\/repos\/crashlytics\/secureudid\/issues\/13\/labels{\/name}","comments_url":"https:\/\/api.github.com\/repos\/crashlytics\/secureudid\/issues\/13\/comments","events_url":"https:\/\/api.github.com\/repos\/crashlytics\/secureudid\/issues\/13\/events","html_url":"https:\/\/github.com\/crashlytics\/secureudid\/issues\/13","id":3923240,"number":13,"title":"Not working with ARC","user":{"login":"SaschaMoellering","id":1321549,"avatar_url":"https:\/\/avatars0.githubusercontent.com\/u\/1321549?v=4","gravatar_id":"","url":"https:\/\/api.github.com\/users\/SaschaMoellering","html_url":"https:\/\/github.com\/SaschaMoellering","followers_url":"https:\/\/api.github.com\/users\/SaschaMoellering\/followers","following_url":"https:\/\/api.github.com\/users\/SaschaMoellering\/following{\/other_user}","gists_url":"https:\/\/api.github.com\/users\/SaschaMoellering\/gists{\/gist_id}","starred_url":"https:\/\/api.github.com\/users\/SaschaMoellering\/starred{\/owner}{\/repo}","subscriptions_url":"https:\/\/api.github.com\/users\/SaschaMoellering\/subscriptions","organizations_url":"https:\/\/api.github.com\/users\/SaschaMoellering\/orgs","repos_url":"https:\/\/api.github.com\/users\/SaschaMoellering\/repos","events_url":"https:\/\/api.github.com\/users\/SaschaMoellering\/events{\/privacy}","received_events_url":"https:\/\/api.github.com\/users\/SaschaMoellering\/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":10,"created_at":"20 
+2

你異常的消息應該有比這更文本。請發佈整個事情。 –

+0

考慮閱讀使用GSON與Volley的官方教程,而不是解析然後解析,然後解析UI線程上的響應.... https://developer.android.com/training/volley/request-custom.html – EpicPandaForce

回答

0

您正嘗試將對象數組(從您的webservices)解析爲包含屬性mResults的對象,該對象是對象數組(Test);

,而不是分析你需要直接使用TestArrayList期間使用Results類:

Results results = gson.fromJson(response.toString(), Results.class); 

應該

ArrayList<Test> results = gson.fromJson(response.toString(), new TypeToken<ArrayList<Test>>(){}.getType()); 

由於ArrayList的是一個通用的它無法使用ArrayList<Test>.class要獲得數據類型的表示,我們必須使用TypeToken。什麼是TypeToken?

TypeToken表示一個泛型類型T. Java尚未提供一種方法來表示泛型類型,因此該類可以。強制客戶創建該類的子類 ,該類允許在運行時檢索類型信息 。

getType返回用於構建TypeToken的類的類型,因此我們可以在gson fromJson()解析方法中使用它。

-

問題二:

你正在使用JsonObjectRequest,但服務器的響應是一個JSON數組,所以你需要使用JsonArrayRequest等更新MyListner使用JSONArray,而不是JSONObject

當您正在分析服務器響應手動你可以使用StringRequest所以避免解析的Volley一步。

StringRequest jsonObjectRequest = new StringRequest(
    Request.Method.GET, 
    "https://api.github.com/repos/crashlytics/secureudid/issues", 
    new MyListner(), 
    new MyErrorListner() 
); 

MyListner現在直接使用String

class MyListner implements Response.Listener<String> { 
    @Override 
    public void onResponse(String response) { 
     Gson gson = new Gson(); 
     ArrayList<Test> results = gson.fromJson(response, new TypeToken<ArrayList<Test>>() {}.getType()); 

     for (Test t : results) { 
      Log.e("Tag", t.toString()); 
     } 
    } 
} 
+0

嘿,感謝您的答案,但仍然無法解決錯誤。我認爲結果班有問題 –

+0

@AkshayNaik確實還有一個錯誤,我更新了我的答案。現在一定是好的(我在一個遊樂場項目上嘗試它,它的工作;)) –

+0

Maisse嘿,感謝解決方案我解決了它我只需要一個更多的幫助,你可以告訴我什麼新的TypeToken >(){} .getType()特別做 –