喜发送表单数据我有一个JSON发送到服务器(POST METHORD){"country":"india","devicetype":"android"}
它是在像这样JSON的关键表单数据模型 是数据即在服务器一样接受它如何retrofit2的Android
正在使用改装我使用多部这样的
@Multipart
@POST("initiate")
@Headers({
"Content-Type: application/json",
"Cache-Control: no-cache"
})
Call<UserInfoServerResponse> getUserInfoRequest(@Part(value="data") UserInfo mUserInfo);
这里的UserInfo是JSON,但我得到之后无法从服务器消息我用FormUrlEncoded methord
@FormUrlEncoded
@POST("initiate")
@Headers({
"Content-Type: application/json",
"Cache-Control: no-cache"
})
Call<UserInfoServerResponse> getUserInfoRequest(@Field(value="data",encoded = false) String mUserInfo);
它放出来,也从服务器相同的失败的结果,但向服务器发送的数据是在甲
data=%7B%22country%22%3A%22india%22%2C%22devicetype%22%3A%22%22%7D
我UserInfo.class
public class UserInfo {
public String country;
public String devicetype;
public UserInfo(String country,String devicetype) {
this.country=country;
this.devicetype=devicetype;
}
}
我的适配器类
RemoteRetrofitInterfaces mService;
Retrofit mRetrofit;
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(20, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS).addInterceptor(interceptor)
.build();
mRetrofit = new Retrofit.Builder()
.baseUrl(AppConstant.HOST).addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
mService = mRetrofit.create(RemoteRetrofitInterfaces.class);
Call<UserInfoServerResponse> api = mService.getUserInfoRequest(new Gson().toJson(mUserInfo));
api.enqueue(new Callback<UserInfoServerResponse>() {
@Override
public void onResponse(Call<UserInfoServerResponse> responseCall, Response<UserInfoServerResponse> response) {
if (response.body().status != null) {
if (response.body().status.equals("success")) {
Log.d(TAG, "success---");
}
} else {
Log.d(TAG, "Failed---");
}
}
@Override
public void onFailure(Call<UserInfoServerResponse> responseCall, Throwable t) {
t.printStackTrace();
}
});
所以我怎么能发送json到服务器使用改造成功我走过了retofit document并按照几个步骤,但我没有得到任何结果。任何一个可以帮我在这
谢谢
安置自己的'UserInfo'类 –
尝试添加 “接受:应用/ JSON” 的标题。 –
你的Retrofit RestAdapter代码在哪里? –