Im trying to get username when I try to login to wev service.I need this username to check did I this right or no.But have NPE when try to get data,but when I just make Toast without data,it works!onResponse work,but I must to get code of this operation to build logic of my app.For example if my code==200,build new activity,else-build alertdialog or something else. What I did wrong? 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) {
}
});
}
}
my interface 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
);
}
and error:
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)