android - 如何使用改造将 json 数据发送到服务器?

标签 android json retrofit

我想将 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/

相关文章:

android - 关闭键盘后 React Native TextInput 仍然具有焦点(TextInput 中的光标)

Android 在第二次更改区域设置后从默认字符串文件 (strings.xml) 加载值

android - 与 Room 和 LiveData 的多对多关系

java - 在表格上方添加图像 - Itext

android - SSL HttpClient 连接中的 session 过期错误

java - 类型 : GWT Compilation Error 没有可用的源代码

javascript - 为什么无法安装此 chrome 扩展程序?

php - Ajax 响应在服务器上是 JSON,而在本地计算机上使用 CakePHP 时是 STRING,为什么?

android - Retrofit 2,解析json,忽略数组内部的错误数据类型

android - 如何通过 Retrofit 解析带有未知 key 的 json?