2014-09-03 30 views
0

我在Android上遇到了WebService問題。我遇到了400錯誤,但ErrorStream中沒有任何信息。在Android中發佈WebService請求

我想要做的是使用JSON的WCF Web服務上的POST請求。

我必須補充說我有includeExceptionDetailInFaults已啓用我的服務。上一次我得到400錯誤,這是因爲我沒有定義RequestProperty。現在我在流中沒有出現任何錯誤。

下面是代碼:

HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
try { 
     // In my last error I had not included these lines. Maybe they are still wrong? 
     urlConnection.setRequestProperty("Content-Type", "application/json"); 
     urlConnection.setRequestProperty("Accept", "application/json"); 
     urlConnection.setRequestMethod("POST"); 

     urlConnection.setDoOutput(true); 
     urlConnection.setChunkedStreamingMode(0); 

     OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream()); 

     OutputStreamWriter outputStreamWriter = new OutputStreamWriter(out); 
     outputStreamWriter.write(jsonObject.toString(), 0, jsonObject.length()); 
     outputStreamWriter.flush(); 
     //outputStreamWriter.close(); 

     int code = urlConnection.getResponseCode(); 
     System.out.println(code); 

     if(code == 400) { 
      BufferedInputStream errorStream = new BufferedInputStream(urlConnection.getErrorStream()); 
      InputStreamReader errorStreamReader = new InputStreamReader(errorStream); 

      BufferedReader bufferedReader = new BufferedReader(errorStreamReader); 
      StringBuilder builder = new StringBuilder(); 
      String aux = ""; 

      while ((aux = bufferedReader.readLine()) != null) { 
        builder.append(aux); 
      } 
      String output = builder.toString(); // The output is empty. 
      System.out.print(output); 
     } 

回答

1

檢查Retrofit庫從廣場是更加輕鬆瘦爲GET/POST請求,並專爲休息。我建議你嘗試一下。它會讓你的生活變得輕鬆。 您可以使用不同的JSON解析器,錯誤處理程序等。非常靈活。使用

POST請求定義改型很簡單這樣的:

一個對象可以用於作爲HTTP請求的身體使用與@Body註解來指定。

@POST("https://stackoverflow.com/users/new") 
void createUser(@Body User user, Callback<User> cb); 

也可以聲明方法來發送表單編碼和多部分數據。

當方法中存在@FormUrlEncoded時,將發送表單編碼數據。每個鍵值對都使用@Field進行註釋,其中包含名稱和提供值的對象。

@FormUrlEncoded 
@POST("/user/edit") 
User updateUser(@Field("first_name") String first, @Field("last_name") String last); 

後您定義的Java接口中的方法像上面顯示實例吧:

RestAdapter restAdapter = new RestAdapter.Builder() 
    .setEndpoint("https://api.soundcloud.com") 
    .build(); 

MyInterface service = restAdapter.create(MyInterface.class); 

然後你就可以同步或異步調用你的方法(如果您傳遞迴調實例)。

service.myapi(requestBody); 

見改造文檔(http://square.github.io/retrofit/javadoc/index.html)和樣品在GitHub的更多細節。

+0

我會看它,並給你答覆。 – 2014-09-03 07:57:20

+0

您是否有一個如何實施帶改造的POST請求的示例?我只能找到GET請求。 – 2014-09-03 08:06:09

+0

同時檢查同一Square團隊的OkHTTP庫(用於網絡連接)。它可以單獨使用或與改造或畢加索圖書館輕鬆集成。它處理默認的Android網絡客戶端之間的差異,並且還具有如下特性:響應緩存,GZIP壓縮,連接重用(池化)等。 – dasar 2014-09-03 08:08:09

0

由於錯誤的URL或錯誤的JSON格式,可能會發生400錯誤(通常發生在我的情況中)。請檢查這兩個

使用的HttpPost對象將使您的工作輕鬆了許多在我看來

HttpPost post = new HttpPost(url); 
    if(payload != null){ 
     try { 
      StringEntity entity = new StringEntity(payload,HTTP.UTF_8); 
      entity.setContentType(contentType); 
      post.setEntity(entity); 
     } catch (UnsupportedEncodingException e) { 
      LOG.d(TAG, "post err url : " + url); 
      LOG.e(TAG, "post err url" , e); 
      throw new Exception(1, e); 
     } 
    } 

    HttpResponse response=executeRequest(owner, post);