2016-12-06 80 views
2

我正在構建一個使用Uber API平臺請求端點的android應用程序。我在HTTPBody中追加數據時遇到了問題,並且它顯示不支持端點等錯誤。如何使用put方法將數據添加到HTTPBody中android

這些curl命令:

curl -X PUT 'https://sandbox-api.uber.com/v1/sandbox/requests/{REQUEST_ID}' 
\ -H 'Content-Type: application/json' 
\ -H 'Authorization: Bearer ' 
\ -d '{"status":"accepted"}' 

代碼:

public JSONObject getStatus(String address, String requestId, String product_id, float start_latitude, float start_longitude, float end_latitude, float end_longitude, String token) { 
     try { 

      httpClient = new DefaultHttpClient(); 
      httpput = new HttpPut("https://sandbox-api.uber.com/v1/requests/"+requestId); 
      **params.add(new BasicNameValuePair("status", "accepted"));** 

      httpput.setHeader("Authorization","Bearer "+token); 
      httpput.setHeader("Content-type", "application/json"); 
      httpput.setEntity(new UrlEncodedFormEntity(params)); 
      HttpResponse httpResponse = httpClient.execute(httpput); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent(); 

     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "iso-8859-1"), 8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "n"); 
      } 
      is.close(); 

      json = sb.toString(); 
      Log.e("JSONStr", json); 
     } catch (Exception e) { 
      e.getMessage(); 
      Log.e("Buffer Error", "Error converting result " + e.toString()); 
     } 

     try { 
      jObj = new JSONObject(json); 
     } catch (JSONException e) { 
      Log.e("JSON Parser", "Error parsing data " + e.toString()); 
     } 

     return jObj; 
    } 
+0

服務器如何期待PUT正文? JSON? XML? URL編碼? – Bhargav

+0

它需要json格式 –

+0

其實我沒有得到如何在HTTPbody中追加狀態 –

回答

1

首先,你要認沽身體要application/json類型的,但你的httpPut對象的實體設置爲UrlEncodedFormEntity 所以你需要先解決這個問題。 首先,你需要創建StringEntity對象,並設置其contentType屬性application/json

在你的情況,因爲你的JSON字符串將是{"status":"accepted"}您需要實例化StringEntity類,像這樣

StringEntity input = new StringEntity("{\"status\":\"accepted\"}"); 

然後設置內容類型,像這樣

input.setContentType("application/json"); 

然後httpput實體屬性設置爲我們剛剛創建的輸入enity像這樣:

httpput.setEntity(input); 

就是這樣只需更換

httpput.setEntity(new UrlEncodedFormEntity(params)); 

與第2行

所以,你的代碼看起來像這樣

代碼:

public JSONObject getStatus(String address, String requestId, String product_id, float start_latitude, float start_longitude, float end_latitude, float end_longitude, String token) { 
    try { 

     httpClient = new DefaultHttpClient(); 
     httpput = new HttpPut("https://sandbox-api.uber.com/v1/requests/"+requestId); 
     httpput.setHeader("Authorization","Bearer "+token); 
     httpput.setHeader("Content-type", "application/json"); 
     // Create the string entity 
     StringEntity input = new StringEntity("{\"status\":\"accepted\"}"); 
     // set the content type to json 
     input.setContentType("application/json"); 
     // set the entity property of the httpput 
     // request to the created input. 
     httpput.setEntity(input); 
     HttpResponse httpResponse = httpClient.execute(httpput); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     is = httpEntity.getContent(); 

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    try { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
       is, "iso-8859-1"), 8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "n"); 
     } 
     is.close(); 

     json = sb.toString(); 
     Log.e("JSONStr", json); 
    } catch (Exception e) { 
     e.getMessage(); 
     Log.e("Buffer Error", "Error converting result " + e.toString()); 
    } 

    try { 
     jObj = new JSONObject(json); 
    } catch (JSONException e) { 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 
    } 

    return jObj; 
} 

如果你想把它帶到下一步,那麼你需要在Java概念中加入Json序列化和反序列化,並學習如何從Java對象生成json字符串,然後你可以將Java對象序列化爲json字符串並實例化StringEntity與生成的json字符串。

+0

然後我也遇到了錯誤,如{「message」:「此方法不支持此端點。」,「code」:「method_not_allowed」},上面的代碼按照你建議的變化, StringEntity input = new StringEntity(「{\」status \「:\」accepted \「}」); input.setContentType(「application/json」); httpput.setEntity(input); –

+0

@RohanChavan那麼這意味着HttpPut不允許嘗試HttpPost而不是 – Bhargav

+0

感謝它的工作 –

相關問題