2017-07-28 113 views
1

我想JSON数据发送到服务器这是在形式如何使用改进将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); 
+0

您可以更改申请的方法'''GET'''。 – KeLiuyue

回答

0

写像下面

@POST("requestQuoteCheck") 
Call<RequestQuoteCheck> requestQuoteCheck(
     @Body HashMap<String,String> data); 

适配器的拦截器像下面

Interceptor interceptor = new Interceptor() { 
      @Override 
      public okhttp3.Response intercept(Chain chain) throws IOException { 
       Request newRequest; 

       newRequest = chain.request().newBuilder() 

         .addHeader("Content-Type","application/json") 
         .method(chain.request().method(), chain.request().body()) 
         .build(); 

       return chain.proceed(newRequest); 
      } 
     }; 

和适配器像下面

public static WebserviceMethods getCommonPathInterfaceRequestQuote() { 
     final OkHttpClient okHttpClient = new OkHttpClient.Builder() 
       .readTimeout(30, TimeUnit.SECONDS) 
       .connectTimeout(30, TimeUnit.SECONDS) 
       .addInterceptor(interceptor); 
       .build(); 
     Retrofit restAdapterRequestQuote = new Retrofit.Builder() 
       .baseUrl(Constants.baseURLforRequestQuote) 
       .addConverterFactory(GsonConverterFactory.create()) 
       .client(okHttpClient) 
       .build(); 
     WebserviceMethods retrofitinterfaces = restAdapterRequestQuote.create(WebserviceMethods.class); 
     return retrofitinterfaces; 
    } 

第二个选项:

@Headers("Content-Type: application/json") 
@POST("requestQuoteCheck") 
Call<RequestQuoteCheck> requestQuoteCheck(
     @Body HashMap<String,String> data); 
+0

仍然不起作用,相同的响应“METHOD NOT ALLOWED” –

+0

检查更新的答案。 –

+0

问题实现Interceptor –

1

使用在字符串键值对传递数据顺便数据的GSON格式瞬间。

gradle这个: -

dependencies { 
    compile 'com.google.code.gson:gson:2.8.0' 
    } 
0

你可以这样做。

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); 

    // edited here 
    Gson gson = new Gson(); 
    String jsonString = gson.toJson(data); 
    RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonString); 
    Call<RequestQuoteCheck> call = RetrofitBaseAdapter.getCommonPathInterfaceRequestQuote().requestQuoteCheck(body); 

    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) { 

     } 
    }); 

请求代码

// edited here ,exchange POST to GET 
@Headers({"Content-Type: application/json","Accept: application/json"}) 
@GET("requestQuoteCheck") 
Call<RequestQuoteCheck> requestQuoteCheck(@Body RequestBody body); 

您必须添加Gson

compile 'com.google.code.gson:gson:2.8.0' 
+0

不工作,相同的响应“方法不允许” –

+0

@GajendraSinghRathore我将''''''''''''改为'''GET'''。 – KeLiuyue

0

试图通过使用多

@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); 

并称之为像下面发送PARAMS通过传球头球

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{ 

} 
相关问题