2016-12-06 44 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

感谢它的工作 –

相关问题