Whats Wrong With This Retrofit Call?
Im trying to send a POST and build a response using retrofit with Android. I have managed to send GET with no problems but now I need to send a POST with some body elements. public
Solution 1:
Well, if you try to send a POST
request with retrofit2
, you should try this:
First, create a Interface, in this interface you declare the body contents for POST request
:
publicinterfaceISesion {
@FormUrlEncoded@POST("/users/login")
Call<Sesion> inSesion(@Field("username") String usuario, @Field("password") String password);
}
The parameter @Field
is the body content that you use in your POST request
Now, you can make this in your MainActivity
:
publicclassMainActivityextendsAppCompatActivity {
privateRetrofit retrofit; //your Retrofit Objectprivate ISesion iSesion; //your interface@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
retrofit = newRetrofit.Builder()
.baseUrl(getString(R.string.url_base)) //your base URL ("http://www.myexample.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
iSesion = retrofit.create(ISesion.class);
Call<Sesion> call = iSesion.inSesion(usuario, contrasena); //you get this fields from editText, for example, and you pass it to the method in your interface
call.enqueue(newCallback<Sesion>() {
@OverridepublicvoidonResponse(Call<Sesion> call, Response<Sesion> response) {
code = response.code();
switch (code) {
case200:
Intent intent = newIntent(getActivity(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
break;
case401:
error = response.body().isError();
mensaje = response.body().getMensaje();
if (error) {
Snackbar.make(view, mensaje, Snackbar.LENGTH_LONG).show();
}
break;
}
}
@OverridepublicvoidonFailure(Call<Sesion> call, Throwable t) {
t.printStackTrace();
Snackbar.make(view, getString(R.string.error_general), Snackbar.LENGTH_LONG).show();
}
});
}
}
The model used in the method Call<Sesion>
is the next:
publicclassSesion {
@SerializedName("error")
privateboolean error;
@SerializedName("message")
privateString mensaje;
publicbooleanisError() {
return error;
}
publicStringgetMensaje() {
return mensaje;
}
}
EDIT:
If you want to send your token in the body content you couldtry this:
Call<Sesion> call = iSesion.inSesion("Basic " + generatedToken); // here is the authentication token
call.enqueue(newCallback<Sesion>() {
@OverridepublicvoidonResponse(Call<Sesion> call, Response<Sesion> response) {
code = response.code();
switch (code) {
case200:
Intent intent = newIntent(getActivity(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
break;
case401:
error = response.body().isError();
mensaje = response.body().getMensaje();
if (error) {
Snackbar.make(view, mensaje, Snackbar.LENGTH_LONG).show();
}
break;
}
}
@OverridepublicvoidonFailure(Call<Sesion> call, Throwable t) {
t.printStackTrace();
Snackbar.make(view, getString(R.string.error_general), Snackbar.LENGTH_LONG).show();
}
});
}
and this is the Interface:
publicinterfaceISesion {
@FormUrlEncoded@POST("/users/login")
Call<Sesion> inSesion(@Field("token") String generatedToken);
}
Post a Comment for "Whats Wrong With This Retrofit Call?"