Java 如何通过 Retrofit 将 Map<String, String> 参数或对象传递给 POST 请求?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21063873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:19:13  来源:igfitidea点击:

How to pass Map<String, String> parameters or object to POST request via Retrofit?

javaandroidapiretrofit

提问by piobab

I have a problem with passing Map parameters or object to Retrofit POST request.

我在将 Map 参数或对象传递给 Retrofit POST 请求时遇到问题。

I follow square, kdubb labstutorials and this threadand I couldn't figure it out.

我遵循squarekdubb labs教程和这个线程,但我无法弄清楚。

My current code which works:

我当前有效的代码:

public interface FacebookUser {
    @FormUrlEncoded
    @POST("/user/login-facebook")
    void login(
            @Field("fb_access_token") String fbAccessToken,
            @Field("os") String os,
            @Field("device") String device,
            @Field("os_version") String osVersion,
            @Field("app_version") String appVersion,
            @Field("online") String online,
            Callback<FacebookLoginUserResponse> callback
    );
}

and code:

和代码:

RestAdapter restAdapter = new RestAdapter.Builder()
                        .setServer(requestMaker.getUrl())
                        .build();

FacebookUser facebookUser = restAdapter.create(FacebookUser.class);
facebookUser.login(getFbAccessToken(),
getString(R.string.config_os),
Info.getAndroidId(getBaseContext()),
Build.VERSION.RELEASE,
        Info.getAppVersionName(getBaseContext()),
        "" + 1,
        new Callback<FacebookLoginUserResponse>() {
    @Override
    public void success(FacebookLoginUserResponse facebookLoginUserResponse, Response response) {
    }

    @Override
    public void failure(RetrofitError retrofitError) {
    }
});

When I try to use this interface I receive from server that parameters are missing:

当我尝试使用此接口时,我从服务器收到缺少参数的消息:

public interface FacebookUser {
    @POST("/user/login-facebook")
    void login(
            @Body Map<String, String> map,
            Callback<FacebookLoginUserResponse> callback
    );
}

and map:

和地图:

HashMap<String, String> map = new HashMap<String, String>();
    map.put("fb_access_token", getFbAccessToken());
    map.put("os", "android");
    map.put("device", Info.getAndroidId(getBaseContext()));
    map.put("os_version", Build.VERSION.RELEASE);
    map.put("app_version", Info.getAppVersionName(getBaseContext()));
    map.put("online", "" + 1);

Questions:What is it wrong? How can I pass object to request?

问题:怎么了?如何将对象传递给请求?

采纳答案by Anton Holovin

Well, now we can implement this thing (version 1.5.0).

好吧,现在我们可以实现这个东西(版本 1.5.0)。

@FormUrlEncoded
@POST("/oauth/access_token")
void getToken(
    @FieldMap Map<String, String> params, 
    Callback<FacebookLoginUserResponse> callback
);

回答by Felix

This feature is still not supported by the Retrofit 1.2.2, however you can compile your own version from the master branch with this feature or wait for the next release.

Retrofit 1.2.2 仍然不支持此功能,但是您可以使用此功能从主分支编译您自己的版本或等待下一个版本。

https://github.com/square/retrofit/pull/390

https://github.com/square/retrofit/pull/390

Update:

更新:

It's available in Retrofit version 1.5.0! (ref Anton Golovinanswer)

它在 Retrofit 版本1.5.0 中可用!(参考Anton Golovin 的回答)

回答by Joolah

In retrofit 2.0 you have to do this way:

在改造 2.0 中,您必须这样做:

@FormUrlEncoded
    @POST(Constant.API_Login)
    Call<UserLoginPost> userLogin(@FieldMap Map<String, String> params);