2015-09-26 215 views
1

我第一次使用Retrofit,使用AsyncTask留下了傳統的httpclient。我對理解改造如何真正起作用存在疑問。改造:預計BEGIN_ARRAY,但在BEGIN_OBJECT第1行第2列

正在此錯誤:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:預期BEGIN_ARRAY但BEGIN_OBJECT位於第1行第2列

完整的URL是:https://api.github.com/users/basil2style

Json return

{ 
    "login": "basil2style", 
    "id": 1285344, 
    "avatar_url": "https://avatars.githubusercontent.com/u/1285344?v=3", 
    "gravatar_id": "", 
    "url": "https://api.github.com/users/basil2style", 
    "html_url": "https://github.com/basil2style", 
    "followers_url": "https://api.github.com/users/basil2style/followers", 
    "following_url": "https://api.github.com/users/basil2style/following{/other_user}", 
    "gists_url": "https://api.github.com/users/basil2style/gists{/gist_id}", 
    "starred_url": "https://api.github.com/users/basil2style/starred{/owner}{/repo}", 
    "subscriptions_url": "https://api.github.com/users/basil2style/subscriptions", 
    "organizations_url": "https://api.github.com/users/basil2style/orgs", 
    "repos_url": "https://api.github.com/users/basil2style/repos", 
    "events_url": "https://api.github.com/users/basil2style/events{/privacy}", 
    "received_events_url": "https://api.github.com/users/basil2style/received_events", 
    "type": "User", 
    "site_admin": false, 
    "name": "Basil", 
    "company": "MakeInfo", 
    "blog": "http://www.themakeinfo.com", 
    "location": "India", 
    "email": "[email protected]", 
    "hireable": true, 
    "bio": null, 
    "public_repos": 39, 
    "public_gists": 3, 
    "followers": 28, 
    "following": 129, 
    "created_at": "2011-12-26T00:17:22Z", 
    "updated_at": "2015-09-23T18:36:51Z" 
} 

這是我的代碼如下。

MainActivity

public class MainActivity extends ActionBarActivity { 

    List<GitHub> examplelist; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     RestAdapter restAdapter = new RestAdapter.Builder(). 
       setEndpoint("https://api.github.com").build(); 
     GitHubAPI gitapi = restAdapter.create(GitHubAPI.class); 
     gitapi.getFeed(new Callback<List<GitHub>>() { 
      @Override 
      public void success(List<GitHub> github, Response response) { 
       examplelist = github; 
       Log.d("Size: ", Integer.toString(examplelist.size())); 
      } 

      @Override 
      public void failure(RetrofitError error) { 
       Log.d("Error: ", error.getLocalizedMessage()); 
      } 
     }); 
    } 

} 

型號

我只是在獲得註冊,ID,avatar_url感興趣,從GitHub API gravater_id數據

public class GitHub { 
    String login; 
    String id; 
    String avatar_url; 
    String gravatar_id; 

    public String getLogin() { 
     return login; 
    } 
    public String getId() { 
     return id; 
    } 
    public String getAvatar_url() { 
     return avatar_url; 
    } 
    public String getGravatar_id() { 
     return gravatar_id; 
    } 
} 

接口

public interface GitHubAPI { 
    @GET("https://stackoverflow.com/users/basil2style") 
    public void getFeed(Callback<List<GitHub>> response); 
} 

請真的需要使這項工作,我很沮喪在這裏。

+0

你問要回了'List'。沒有'List'。如果你看看你的JSON,它不是一個JSON數組。它是一個JSON對象。要麼你有錯誤的URL,要麼你需要調整你的代碼,以期望只有一個'GitHub'實例,而不是N. – CommonsWare

+0

返回的數據是一個Map/Dictionary,而不是一個List。 –

+0

@KentHawkings,我不知道。 (我想我需要學習更多。謝謝反正。 –

回答

1

你的界面應該是這樣的:

public interface GitHubAPI { 
    @GET("https://stackoverflow.com/users/basil2style") 
    public void getFeed(Callback<GitHub> response); 
} 

和您的回調:

gitapi.getFeed(new Callback<GitHub>() { 
      @Override 
      public void success(GitHub github, Response response) { 
       Github example = github; 
       //Log.d("Size: ", Integer.toString(examplelist.size())); 
      } 

      @Override 
      public void failure(RetrofitError error) { 
       Log.d("Error: ", error.getLocalizedMessage()); 
      } 
     }); 
+0

謝謝你你的回答,我更困擾爲什麼這是downvoted。 –

+0

這就是我正在尋找的答案,謝謝! –

相關問題