试图获取POST方法Retrofit的数据

时间:2016-04-25 10:28:33

标签: android retrofit retrofit2

当我尝试登录wev服务时,我试图获取用户名。我需要这个用户名来检查我这是对还是没有。但是当尝试获取数据时有NPE,但是当我只做Toast没有数据时,它的工作原理!onResponse工作,但我必须获取此操作的代码来构建我的app的逻辑。例如,如果我的代码== 200,构建新活动,else-build alertdialog或其他。 我做错了什么? 的 myFragment:

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

        String username_value = username.getText().toString();
        String 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) {
        Toast.makeText(getActivity(), "", Toast.LENGTH_SHORT).show();
      String response_value =   response.body().signInResults.get(0).getUsername();
        Log.d("FeedFragment",response_value);
    }

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

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

和错误:

FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.vid_me_app.FeedFragment$2.onResponse(FeedFragment.java:59)
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)

1 个答案:

答案 0 :(得分:2)

SignInResults.java //模型类

clas SignInResults {
   public Boolean status;
   public Auth auth;
   public User user;
}

User.java //用户模型

public class User {
    public String userId;
    public String username;
    public String avatar;
}

在onResponse函数中签出此代码

if (response.isSuccess()) {

    SignInResults results = response.body();

    if(results != null) {

        String userName = results.user.username;

        Log.d("User Name ==>> ", userName);

    }

} else {
    // Do whatever you want if API is unsuccessful.
}