Java – how to pass map parameters or objects to post requests through transformation?
•
Java
I encountered a problem passing a map parameter or object to a retrofit post request
I follow square, kdubb labs tutorial and this thread, I can't figure it out
How my current code 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 a missing parameter from the server:
public interface FacebookUser { @POST("/user/login-facebook") void login( @Body Map<String,String> map,Callback<FacebookLoginUserResponse> callback ); }
And maps:
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);
Question: what's the matter? How to pass object requests?
Solution
So 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 );
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码