我将Map参数或对象传递给Retrofit POST请求时遇到问题.
我遵循square,kdubb labs教程和this thread,我无法弄清楚.
我目前的代码工作原理:
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 ); }
和代码:
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) { } });
当我尝试使用这个接口时,我从服务器收到参数丢失:
public interface FacebookUser { @POST("/user/login-facebook") void login( @Body Map<String,String> map,Callback<FacebookLoginUserResponse> callback ); }
和地图:
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);
问题:
怎么了?
如何传递对象请求?
解决方法
那么现在我们可以实现这个东西(1.5.0版).
@FormUrlEncoded @POST("/oauth/access_token") void getToken( @FieldMap Map<String,String> params,Callback<FacebookLoginUserResponse> callback );