【问题标题】:NPE in onResponse method using Retrofit使用 Retrofit 的 onResponse 方法中的 NPE
【发布时间】:2016-04-25 19:46:23
【问题描述】:

我正在尝试通过 Retrofit 库从 onResponse 方法获取数据。但现在我有了响应 ==0。我需要获取我的请求代码,以便在我的应用程序中构建逻辑。例如,如果代码 ==200,开始新活动是其他构建错误消息。 我正在尝试为 vid.me 服务构建正确的 POST 方法。该服务具有开放的 API,我需要的方法是: https://docs.vid.me/#api-Auth-Create 我的片段:

public class FeedFragment extends Fragment {
    EditText username;
    EditText password;
    Button btnLogin;

    public List<SignInResult> signInResult;
    String username_value,password_value;
    public static final String ROOT_URL = "https://api.vid.me/";

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_feed, container, false);
        username = (EditText) rootView.findViewById(R.id.user_name_field);
        password = (EditText) rootView.findViewById(R.id.password_field);
        btnLogin = (Button) rootView.findViewById(R.id.button_login);
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Authorize();
            }
        });
        return rootView;
    }

    public void Authorize() {
        Retrofit retrofitAdapter = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(ROOT_URL)
                .build();
        final VideoApi videoApi = retrofitAdapter.create(VideoApi.class);

         username_value = username.getText().toString();
         password_value = password.getText().toString();

        Call<SignInResults> call = videoApi.insertUser(username_value,password_value);
        call.enqueue(new Callback<SignInResults>() {


            @Override
            public void onResponse(Call<SignInResults> call, Response<SignInResults> response) {
                SignInResults results = response.body();
                Log.d("Response ==>> ", new GsonBuilder().setPrettyPrinting().create().toJson(results));

            }

            @Override
            public void onFailure(Call<SignInResults> call, Throwable t) {

            }
        });
}
}

API接口:

public interface VideoApi {

    @GET("/videos/featured")
    Call<Videos> getFeaturedVideo();

    @GET("/videos/new")
    Call<Videos> getNewVideo();

@Headers("Content-Type:application/x-www-form-urlencoded")
    @FormUrlEncoded
    @POST("/auth/create")
   Call<SignInResults>insertUser(@Field("email") String username,
                           @Field("password") String password
                           );
}

SignInResult 类:

public class SignInResult {
    public String getAuthorization() {
        return authorization;
    }

    public void setAuthorization(String authorization) {
        this.authorization = authorization;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @SerializedName("authorization")
    @Expose
    private String authorization;
    @SerializedName("code")
    @Expose
    private String code;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @SerializedName("username")
    @Expose
    private String username;

    public String getUser_id() {
        return user_id;
    }

    public void setUser_id(String user_id) {
        this.user_id = user_id;
    }

    @SerializedName("user_id")
    @Expose
    private String user_id;
}

SignInResults 类:

public class SignInResults {
public SignInResult signInResult;

    public List<SignInResult> getSignInResults() {
        return signInResults;
    }

    List<SignInResult> signInResults;
}

【问题讨论】:

  • 能提供NPE异常的stacktrace吗?

标签: android retrofit retrofit2


【解决方案1】:

您必须更改此代码才能正确处理结果

Call<SignInResults> call = videoApi.insertUser(username_value,password_value);
    call.enqueue(new Callback<SignInResults>() {

        @Override
        public void onResponse(Call<SignInResults> call, Response<SignInResults> response) {
            if(response.isSuccessful()) {
                SignInResults results = response.body();
                Log.d("Response ==>> ", new GsonBuilder().setPrettyPrinting().create().toJson(results));
            }
            else {
                // handle error
            }
        }

        @Override
        public void onFailure(Call<SignInResults> call, Throwable t) {

        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-29
    • 2018-05-30
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多