2015-09-26 144 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

这就是我正在寻找的答案,谢谢! –

相关问题