首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我无法解析改装响应

我无法解析改装响应
EN

Stack Overflow用户
提问于 2018-02-19 10:01:28
回答 3查看 449关注 0票数 0

我创建了一个公共类来访问所有POJO类。当我试图解析和访问POJO变量时,我得到的是空指针异常。我真的不知道我哪里错了。有人能帮我解决这个问题吗?

CommonReponse.java

代码语言:javascript
运行
复制
public class CommonResponse {

private Github github;

public Github getGithub() {
    return github;
}

public void setGithub(Github github) {
    this.github = github;
}}

GithubService.java

代码语言:javascript
运行
复制
public interface GithubService {

@GET("users/{username}")
Call<CommonResponse> getGithubUserInfo(@Path("username") String username);}

Github.java // POJO

代码语言:javascript
运行
复制
public class Github {

@SerializedName("name")

private String name;

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}}

GetUserInfo() // Method

代码语言:javascript
运行
复制
   private void getUserInfo() {
    Retrofit retrofit = new Retrofit.Builder()
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl("https://api.github.com/")
            .build();

    GithubService githubService = retrofit.create(GithubService.class);
    Call<CommonResponse> call = githubService.getGithubUserInfo(mGitUserName);
    call.enqueue(new retrofit2.Callback<CommonResponse>() {
        @Override
        public void onResponse(Call<CommonResponse> call, Response<CommonResponse> response) {

            System.out.println("Response:+"+response.body().getGithub().getName());

        }

        @Override
        public void onFailure(Call<CommonResponse> call, Throwable t) {
            System.out.println("Response error:+"+t.getMessage());

        }
    });}

日志

代码语言:javascript
运行
复制
02-19 15:38:53.926 32293-32293/com.ananth.rxandroidwithretrofit E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.ananth.rxandroidwithretrofit, PID: 32293
                                                                              java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.ananth.rxandroidwithretrofit.model.Github.getName()' on a null object reference
                                                                                  at com.ananth.rxandroidwithretrofit.ProfileActivity$7.onResponse(ProfileActivity.java:243)
                                                                                  at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
                                                                                  at android.os.Handler.handleCallback(Handler.java:751)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                  at android.os.Looper.loop(Looper.java:154)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6682)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
EN

Stack Overflow用户

发布于 2018-02-19 10:13:53

尝尝这个

代码语言:javascript
运行
复制
private void getUserInfo() {
Retrofit retrofit = new Retrofit.Builder()
        .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
        .addConverterFactory(GsonConverterFactory.create())
        .baseUrl("https://api.github.com/")
        .build();

GithubService githubService = retrofit.create(GithubService.class);
Call<Github> call = githubService.getGithubUserInfo(mGitUserName);
call.enqueue(new retrofit2.Callback<Github>() {
    @Override
    public void onResponse(Call<Github> call, Response<Github> response) {

        System.out.println("Response:+"+response.body().getName());

    }

    @Override
    public void onFailure(Call<Github> call, Throwable t) {
        System.out.println("Response error:+"+t.getMessage());

    }
});}

代码语言:javascript
运行
复制
public interface GithubService {

@GET("users/{username}")
Call<Github> getGithubUserInfo(@Path("username") String username);}
票数 0
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48863594

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档