我想将 JSON 数据发送到表单中的服务器
{
"comments": "Testing",
"quantity": 0,
"retailerId": 0,
"retailerquote": 0,
"subProductId": 999,
"unit": "kg",
"wholesalerid": 999
}
当它实际发布时,它会返回“已添加”作为响应,我不知道该代码有什么问题,它向我显示“方法不允许”。 我就是这样做的。
HashMap<String,String> header = new HashMap<String, String>();
header.put("Content-type","application/json");
HashMap<String,String> data = new HashMap<String, String>();
data.put("comments",comments);
data.put("quantity",quantity);
data.put("retailerId",retailerID);
data.put("retailerquote",retailerQuote);
data.put("wholesalerId",wholesalerID);
data.put("unit",unit);
data.put("subProductId",subProductID);
Call<RequestQuoteCheck> call = RetrofitBaseAdapter.getCommonPathInterfaceRequestQuote().requestQuoteCheck(data);
call.enqueue(new retrofit2.Callback<RequestQuoteCheck>() {
@Override
public void onResponse(Call<RequestQuoteCheck> call, Response<RequestQuoteCheck> response) {
Toast.makeText(getApplicationContext(),response.message(),Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<RequestQuoteCheck> call, Throwable t) {
}
});
这是基础适配器类
public static WebserviceMethods getCommonPathInterfaceRequestQuote() {
final OkHttpClient okHttpClient = new OkHttpClient.Builder()
.readTimeout(30, TimeUnit.SECONDS)
.connectTimeout(30, TimeUnit.SECONDS)
.build();
Retrofit restAdapterRequestQuote = new Retrofit.Builder()
.baseUrl(Constants.baseURLforRequestQuote)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
WebserviceMethods retrofitinterfaces = restAdapterRequestQuote.create(WebserviceMethods.class);
return retrofitinterfaces;
}
}
这是 webServiceMethods.java 文件
@POST("requestQuoteCheck")
Call<RequestQuoteCheck> requestQuoteCheck(
@HeaderMap Map<String,String> data );
最佳答案
尝试使用 multipart 发送参数
@Multipart
@POST("/QuickBroker/broker/uploadDocuments")
Call<ResponseBody> uploadFile(@Part("comments") RequestBody comments,@Part("quantity") RequestBody quantity,@Part("retailerId") RequestBody retailerId,@Part("retailerquote") RequestBody retailerquote,@Part("wholesalerId") RequestBody wholesalerId);
然后通过传递 header 像下面这样调用它
RequestBody mobile = RequestBody.create(MediaType.parse("multipart/form-data"), pref.getString(AppConstants.MOBILE_NUMBER,""));
if(body1 != null && body2 != null && body3 != null && body4 != null) {
RetrofitAPIs retrofitAPIs = RetrofitBuilders.getInstance().getAPIService(RetrofitBuilders.getBaseUrl());
Call call = retrofitAPIs.uploadFile(param1, param2, param3, param4, param5);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
pd.dismiss();
}
});
}else{
}
关于android - 如何使用改造将 json 数据发送到服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45366420/