【发布时间】:2016-02-29 07:04:06
【问题描述】:
嗨,我有一个要发送到服务器的 json (POST METHORD){"country":"india","devicetype":"android"} 它是表单数据模型
就像这个 json 的关键是 data 即服务器是否像
data={"country":"india","devicetype":"android"} 正在使用改造我使用这样的 Multipart
@Multipart
@POST("initiate")
@Headers({
"Content-Type: application/json",
"Cache-Control: no-cache"
})
Call<UserInfoServerResponse> getUserInfoRequest(@Part(value="data") UserInfo mUserInfo);
这里 UserInfo 是 json,但在我使用 FormUrlEncoded 方法之后我从服务器收到失败消息
@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课程 -
尝试在标题中添加“Accept: application/json”。
-
你的 Retrofit RestAdapter 代码在哪里?
-
好吧,这是我的用户信息类
-
请检查我的答案是否相同。我会发帖