Retrofit Post Request With Body Parameters Android
Solution 1:
First you need to create request( POJO Class)
publicclassFeedbackRequest{
publicString email;
publicString feedback;
}
when you call sendFeedbackRequest()
pass the FeedbackRequest
like below"
FeedbackRequestreq=newFeedbackRequest();
req.email= "email";
req.feedback= "feedback"
sendFeedbackRequest(req)
after that your sendFeedbackRequest()
should be like this
privatevoidsendFeedbackRequest(FeedbackRequest request){
API.request().sendFeedback(request).enqueue(newCallback<String>() {
@OverridepublicvoidonResponse(Call<String> call, Response<String> response) {
goToMainActivity();
}
@OverridepublicvoidonFailure(Call<String> call, Throwable t) {
Toast.makeText(SplashScreenActivity.this, R.string.try_again_later, Toast.LENGTH_SHORT).show();
}
});
And your retrofit request should be like this,
@FormUrlEncoded@POST("api/android-feedback")
@Headers({"Content-Type: application/json", "Authorization: F31daaw313415"})
Call<String> sendFeedback(@Body FeedbackRequest request);
Now it should work. feel free to ask anything.
Solution 2:
You are using a Gson converter factory. It might be easier to create a single object that represents your body, and use that instead of all individual parameters. That way, you should be able to simple follow along with the examples on the Retrofit website.enter link description here There are also many site that let you generate your Plain Old Java Objects for you, like this one:
E.g. your Api call:
@POST("api/android-feedback")
Call<String> sendFeedback(@Body FeedbackBody feedback);
And your FeedbackBody class:
publicclassFeedbackBody{
privatefinal String email;
privatefinal String feedback;
public FeedbackBody(String email, String feedback){
this.email = email;
this.feedback = feedback;
}
}
Solution 3:
Java:
@POST("/api/android-feedback")
Call<String> sendFeedback(@Body FeedbackBody feedback);
Kotlin:
@POST("/api/android-feedback")
fun sendFeedback(@Bodyfeedback: FeedbackBody): Call<String>
Also, probably you forgot leading slash in the endpoint.
Post a Comment for "Retrofit Post Request With Body Parameters Android"