我无法解析改装响应

时间:2018-02-19 10:01:29

标签: android parsing retrofit2

我创建了一个公共类来访问所有POJO类。当我尝试解析和加入POJO变量时,我得到空指针异常。我真的不知道我错在哪里。任何人都可以帮我解决这个问题吗?

CommonReponse.java

public class CommonResponse {

private Github github;

public Github getGithub() {
    return github;
}

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

GithubService.java

public interface GithubService {

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

Github.java // POJO

public class Github {

@SerializedName("name")

private String name;

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

GetUserInfo()//方法

   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());

        }
    });}

登录

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)

4 个答案:

答案 0 :(得分:0)

问题是getGitHub会返回null。原因是当您尝试获取githubCommonResponse实例的name成员尚未初始化。解决方案是以这种方式修改getGitHub

public Github getGithub() {
    if (github == null) {
        //Initialize github, so it will not be null
    }
    return github;
}

答案 1 :(得分:0)

初始化您的模型

Github model; //In your Activity

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) {

            if(response!=null)
            {
            model = response.body(); //Parse your response here
            }

        }

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

        }
    });}

答案 2 :(得分:0)

试试这个

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());

    }
});}

public interface GithubService {

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

答案 3 :(得分:0)

更改你的POJO课程。不要设置getter和setter。

public class CommonResponse {
 @SerializedName("github")
 @Expose
 public Github github;
}


public class Github {
 @SerializedName("name")
 @Expose
 public String name;
}