NPE尝试登录Web服务

时间:2016-04-25 05:06:52

标签: android retrofit retrofit2

我尝试使用用户名和密码登录屏幕以连接到名为vid.me的网络服务。该服务具有API https://docs.vid.me/#api-Auth-Create

通过这种方法我必须登录我的app.Im试图获取一些数据当我登录Log进行检查我是否正确行事,但NullPointerException

Error:
java.lang.NullPointerException
at com.example.vid_me_app.FeedFragment$2.onResponse(FeedFragment.java:56)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:5751)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
at dalvik.system.NativeStart.main(Native Method)

API接口:

public interface VideoApi {

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

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

    @FormUrlEncoded
    @POST("/auth/create")
   Call<SignInResults>insertUser(@Field("username") String username,
                           @Field("password") String password
                           );
}

我使用Retrofit的片段:

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

                signInResult = response.body().signInResults;
                Log.d("FeedFragment", "Username = " + signInResult.get(0).getUsername());
            }

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

            }
        });

    }
}

0 个答案:

没有答案