简体   繁体   English

如何传递地图 <String, String> 参数或对象通过Retrofit POST请求?

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

I have a problem with passing Map parameters or object to Retrofit POST request. 我将Map参数或对象传递给Retrofit POST请求时遇到问题。

I follow square , kdubb labs tutorials and this thread and 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? 如何将对象传递给请求?

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
);

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);

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 ! 它在Retrofit 1.5.0版本中可用! (ref Anton Golovin answer) (参考Anton Golovin回答)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM