Retrofit POST Request with body parameters android

后端 未结 3 1289
Happy的楠姐
Happy的楠姐 2021-01-28 05:25

I need to execute post request with retrofit but i have a problem which i can\'t understand very well. Before trying with code i tested api call with Postman an

3条回答
  •  广开言路
    2021-01-28 05:55

    First you need to create request( POJO Class)

    public class FeedbackRequest {
       public String email;
       public String feedback;
    }
    

    when you call sendFeedbackRequest() pass the FeedbackRequest like below"

    FeedbackRequest req = new FeedbackRequest();
    req.email= "email";
    req.feedback= "feedback"
    sendFeedbackRequest(req)
    

    after that your sendFeedbackRequest() should be like this

      private void sendFeedbackRequest(FeedbackRequest request){
          API.request().sendFeedback(request).enqueue(new Callback() {
          @Override
          public void onResponse(Call call, Response response) {
            goToMainActivity();
          }
    
          @Override
          public void onFailure(Call 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 sendFeedback(@Body FeedbackRequest request);
    

    Now it should work. feel free to ask anything.

提交回复
热议问题