用身体参数android改造POST请求 [英] Retrofit POST Request with body parameters android

查看:314
本文介绍了用身体参数android改造POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要执行带有更新的请求,但是我有一个我不太了解的问题.在尝试代码之前,我使用 Postman 测试了api调用,并请求如下所示:

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 and request look like this:

这是我的android代码:

Here is my android code:

public class API {

private static <T> T builder(Class<T> endpoint) {

    return new Retrofit.Builder()
            .baseUrl(Utils.API_BASE_URL)
            .addConverterFactory(ScalarsConverterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build()
            .create(endpoint);
}
public static AllRequests request() {
    return builder(AllRequests.class);
}
}

编辑请求:

@POST("api/android-feedback")
@Headers({"Content-Type: application/x-www-form-urlencoded", "Authorization: F#@3FA@#Rad!@%!2s"})
Call<String> sendFeedback(@Body FeedbackBody body);

FeedbackBody:

FeedbackBody:

public class FeedbackBody{
private final String email;
private final String feedback;

public FeedbackBody(String email, String feedback){
    this.email = email;
    this.feedback = feedback;
}

}

最后我构造了请求并等待响应,问题是我在onFail方法中接收到消息

And finally i construct the request and wait for response, the problem is that i receive message in onFail method

   private void sendFeedbackRequest(){
    API.request().sendFeedback(new FeedbackBody("testmeil@meil.com", "test feedback").enqueue(new Callback<String>() {
        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            goToMainActivity();
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            Toast.makeText(SplashScreenActivity.this, R.string.try_again_later, Toast.LENGTH_SHORT).show();
        }
    });

仍然无法正常工作..我认为我想出了问题所在,因为服务器端等待简单的POST请求而无需Json格式,我认为Retrofit默认使用JSON格式,如果我发送POST请求并使用JSON格式Body参数服务器将无法解析我的请求,是否有可能像在POSTMAN一样发送简单的POST请求而无需使用JSON格式化?

Still not working.. i think i figure it out where can be the problem, because server side wait for simple POST request without Json formatting, i think Retrofit use JSON formatting by default, and if i send POST request and format Body parameters with JSON the server will fail to parse my request, is there any chance to send simple POST request like at POSTMAN without formatting with JSON ?

  • 要发送的Php api等待请求:

$ _ POST ['feedback'] ='blabla'; $ _POST ['email'] ='blabla ..';

$_POST['feedback'] = 'blabla'; $_POST['email'] = 'blabla..';

如果他收到Json格式的请求就无法解析它,因此我收到失败响应.

and if he receive Json format request can't parse it and because of that i receive fail response.

推荐答案

首先,您需要创建请求(POJO类)

First you need to create request( POJO Class)

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

当您呼叫sendFeedbackRequest()时,将FeedbackRequest传递如下所示"

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

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

之后,您的sendFeedbackRequest()应该像这样

  private void sendFeedbackRequest(FeedbackRequest request){
      API.request().sendFeedback(request).enqueue(new Callback<String>() {
      @Override
      public void onResponse(Call<String> call, Response<String> response) {
        goToMainActivity();
      }

      @Override
      public void onFailure(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.

这篇关于用身体参数android改造POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆