2017-07-28 136 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{ 

} 
相關問題