我第一次使用Retrofit,留下了传统的httpclient和AsyncTask。我在理解Retrofit如何运作方面遇到了问题。
收到此错误: com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_ARRAY但在第1行第2列为BEGIN_OBJECT
完整网址为:https://api.github.com/users/basil2style
Json返回
{
"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": "basiltalias92@gmail.com",
"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());
}
});
}
}
模型
我只对从Github api获取login,id,avatar_url和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("/users/basil2style")
public void getFeed( Callback<List<GitHub>> response);
}
我真的需要做这项工作,我很沮丧。
答案 0 :(得分:1)
您的界面应如下所示:
public interface GitHubAPI {
@GET("/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());
}
});