【问题标题】:Retrofit2 (Android) - How to send form-data along POST methodRetrofit2 (Android) - 如何通过 POST 方法发送表单数据
【发布时间】:2018-01-07 16:10:23
【问题描述】:

*P.s:更改了安全问题的参考。

我正在尝试从服务器获取 json,但它需要请求正文中的表单数据,如下所示:

在邮递员中测试时它可以工作,但我只是不知道如何使用retrofit2来做到这一点。

豆子:

public class Y {

@SerializedName("id")
private int yId;
@SerializedName("name")
private String yName;


//...
}


public class YList {

@SerializedName("ys")
private List<Y> ys;

//...
}

服务接口是这样的:

public interface YService {
@POST("y")
Call<YList> getY()

public static final YService serviceY = new Retrofit.Builder()
        .baseUrl("http://example.com.br/api/x/")
        .addConverterFactory(GsonConverterFactory.create())
        .build()
        .create(YService.class);
}

和REST方法:

YService yService = YService.serviceY;




    yService.getY().enqueue(new Callback<YList>() {
        @Override
        public void onResponse(Call<YList> call, Response<YList> response) {

            if (response.isSuccessful()) {
                //...
            } else {

                //...

            }

        }

        @Override
        public void onFailure(Call<YList> call, Throwable t) {

            //...

        }
    });'

邮递员 JSON 结果:

{
"auth": {
  "validation": true
},
"ys": [
{
  "id": 1,
  "name": "#"
}
]
}

【问题讨论】:

  • 你的界面在哪里?您的 Call> 在哪里?放更多信息
  • 已添加代码。希望可以有人帮帮我。 @KostasDrak
  • 你读过@Body 改造注解吗?
  • 我尝试使用 getY(@Body AuthRequest.Auth info)。其中 AuthRequest 有一个带有电子邮件和密码属性的“Auth”内部类,但这不起作用。我想这是因为没有引用表单数据键名? @KostasDrak

标签: android request http-post retrofit retrofit2


【解决方案1】:

Retrofit2 Body with from-data

@FormUrlEncoded 
@POST("xxx")//endpoint
Call<xxxx> getxxx(@Field("phone") String phone);

【讨论】:

    【解决方案2】:

    我设法找到了一个解决方案,结果比我想象的要容易得多。

    只需像这样创建一个名为 AuthRequest 的类:

    public class AuthRequest {
    
        private static final String TAG = "Auth";
        private static final String EMAIL = "email";
        private static final String PASSWORD = "pass";
    
    
        //creates a json-format string
        public static String createAuthJsonString(){
    
            String info = "{\"auth\":{\"email\":\""+EMAIL+"\",\"password\":\""+PASSWORD+"\"}}";
    
    
            Log.i(TAG,info);
    
            return info;
        }
    
    }
    

    只需将@FormUrlEncoded 添加到@Post 并将@Field 键和值放入方法调用中:

    @FormUrlEncoded
    @POST("ys")//endpoint
    Call<YList> getY(@Field("info") String info);
    
    
    // Connection url.
    public static final YService serviceY = new Retrofit.Builder()
            .baseUrl("http://example.com.br/api/x/")
            .addConverterFactory(GsonConverterFactory.create())
            .build()
            .create(YService.class);
    

    使用 getY() 方法:

        final YService yService = YService.serviceY;
        //...
        yService.getY(AuthRequest.createAuthJsonString()).enqueue(new 
            Callback<YList>() {
    
            @Override
            public void onResponse(Call<YList> call, Response<YList> response) {
    
                if (response.isSuccessful()) {
                  //...
    
                } else {
    
                   //...
                }
            }
    
            @Override
            public void onFailure(Call<YList> call, Throwable t) {
    
                //...
    
            }
    
    
        });
    
    
    
    }
    

    从 json 完美返回 YList。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-23
      • 2018-06-13
      • 2016-03-11
      • 1970-01-01
      • 2015-08-04
      • 2020-09-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多