Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

I follow square, kdubb labs tutorials and this thread and I couldn't figure it out.

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?

like image 736
piobab Avatar asked Jan 11 '14 15:01

piobab


People also ask

How do you add a string value to a string map?

Maps are associative containers that store elements in a specific order. It stores elements in a combination of key values and mapped values. To insert the data in the map insert() function in the map is used.

What is@ body in Retrofit?

The @Body annotation defines a single request body. interface Foo { @POST("/jayson") FooResponse postJson(@Body FooRequest body); } Since Retrofit uses Gson by default, the FooRequest instances will be serialized as JSON as the sole body of the request.


2 Answers

In retrofit 2.0 you have to do this way:

@FormUrlEncoded
    @POST(Constant.API_Login)
    Call<UserLoginPost> userLogin(@FieldMap Map<String, String> params);
like image 124
Thiago Avatar answered Sep 23 '22 22:09

Thiago


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

@FormUrlEncoded
@POST("/oauth/access_token")
void getToken(
    @FieldMap Map<String, String> params, 
    Callback<FacebookLoginUserResponse> callback
);
like image 26
Anton Holovin Avatar answered Sep 21 '22 22:09

Anton Holovin