问题描述
我有一个要发送到服务器的json(开机自检){"country":"india","devicetype":"android"}
,它处于表单数据模型中就像这个json的密钥是 data ,即服务器是否接受
Hi i have a json to send to the server (POST METHORD){"country":"india","devicetype":"android"}
it is in form data modellike the key for this json is data ie is the server accept it like
data={"country":"india","devicetype":"android"}
正在使用翻新,我使用了这样的Multipart
data={"country":"india","devicetype":"android"}
am using retrofit i use Multipart like this
@Multipart
@POST("initiate")
@Headers({
"Content-Type: application/json",
"Cache-Control: no-cache"
})
Call<UserInfoServerResponse> getUserInfoRequest(@Part(value="data") UserInfo mUserInfo);
这里 UserInfo 是json,但是在我使用FormUrlEncoded方法之后,服务器发出了失败消息
here UserInfo is the json but am getting fail message from server after that i used FormUrlEncoded methord
@FormUrlEncoded
@POST("initiate")
@Headers({
"Content-Type: application/json",
"Cache-Control: no-cache"
})
Call<UserInfoServerResponse> getUserInfoRequest(@Field(value="data",encoded = false) String mUserInfo);
它的输出也是来自服务器的相同失败结果,但是发送到服务器的数据在格式中
its out put is also same failure result from server, but the data sending to the server is in the formate
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发送到服务器,我通过了改造文档,然后遵循几个步骤,但我没有任何结果.有人可以帮我吗
so how can i send the json to server using retrofit successfully i gone through the retofit document and follow couple of steps but i dont get any result. can any one help me in this
谢谢
推荐答案
最后,我找到了解决方案,希望对其他人有帮助
finally i found the solution hope this will help some other
我通过使用 FieldMap
@POST("initiate")
@FormUrlEncoded
Call<UserInfoServerResponse> getUserInfoRequest(@FieldMap Map<String,String> params);
在"Rest Adapter"部分中,我将请求数据从字符串更改为Hashmap形式,如下所示:
and in the Rest Adaptor section i changed request data from string to Hashmap form like following
Log.d(TAG, "sendUserInfo called");
UserInfo mInfo=new UserInfo("countyname","android");
String request=new Gson().toJson(mUserInfo);
//Here the json data is add to a hash map with key data
Map<String,String> params = new HashMap<String, String>();
params.put("data", request);
Call<UserInfoServerResponse> api = mService.getUserInfoRequest(params);
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---" + response.body());
}
} else {
Log.d(TAG, "Failed---");
}
}
@Override
public void onFailure(Call<UserInfoServerResponse> responseCall, Throwable t) {
t.printStackTrace();
}
});
从根本上说,我使用了 @FormUrlEncoded 来获取表单数据,并使用了 @FieldMap 来将我的请求JSON用作键值.我通过遵循这种方法得到了解决方案,希望对您有所帮助:)
这篇关于如何在Retrofit2 Android中发送表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!