使用翻新的NPE错误 [英] NPE error using Retrofit

查看:107
本文介绍了使用翻新的NPE错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想登录一些名为vid.me的服务, https://api.vid.me/oauth/authorize 与POST.但是,当我尝试从日志中获取数据时,我遇到了NullPointerException.我也尝试制作Toast并也出现了此错误.

I want to login in some service called vid.me,https://api.vid.me/oauth/authorize with POST.But when I try to get data from log I have NullPointerException.I tryed to make Toast and have this error too.I'm trying to get response code to see I did this right or no.

我的API类:

public interface VideoApi {

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

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

    @FormUrlEncoded
    @POST("oauth/authorize")
   Call<SignInResults>insertUser(@Field("name") String name,
                           @Field("password") String password
                           );
}

我的片段:

public class FeedFragment extends Fragment {
    EditText username;
    EditText password;
    Button btnLogin;
    public List<SignInResult> signInResult;
    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);
       Call<SignInResults> call = videoApi.insertUser(username.getText().toString(),password.getText().toString());
        call.enqueue(new Callback<SignInResults>() {
            @Override
            public void onResponse(Call<SignInResults> call, Response<SignInResults> response) {
                Log.d("FeedFragment", "Status Code = " + response.body().signInResults.get(0).getCode());
            }

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

            }
        });

    }
}

推荐答案

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


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

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

    }
});



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

这篇关于使用翻新的NPE错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆