2016-12-06 10 views
2

Uber APIライドリクエストエンドポイントを使用するアンドロイドアプリを構築しています.HTTPBodyでデータを追加する際に問題があり、エンドポイントがサポートされていないなどのエラーが表示されます。androidにputメソッドを使用してデータを追加する方法

これらは、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本体をどのように想定していますか?ジュソン? xml? URLエンコードされた? – Bhargav

+0

json形式が必要です –

+0

実際にHTTPbodyにステータスを追加する方法が得られていません –

答えて

1

まずあなたがタイプapplication/jsonであるとPUTのボディにしたいが、あなたはそうUrlEncodedFormEntityhttpPutオブジェクトの実体を設定しています最初にこれを修正する必要があります。 まず、あなたがそのように

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

などStringEntityクラスをインスタンス化する必要が{"status":"accepted"}になるだろう、あなたのJSON文字列ので、あなたのケースではapplication/json

にそのcontentTypeプロパティをStringEntityオブジェクトを作成し、設定する必要があります次に、先ほど作成した入力enityにhttpputエンティティプロパティを設定するので、

input.setContentType("application/json"); 

などのコンテンツタイプを設定しますそのよう:だ

httpput.setEntity(input); 

それだけで最初の2行で

httpput.setEntity(new UrlEncodedFormEntity(params)); 

を交換

だからあなたのコードは、この

コードのようになります。

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

これを次のステップに進めたい場合は、Jsonのシリアル化とJavaの概念の逆シリアル化を行い、Javaオブジェクトからjson文字列を生成する方法を学び、次にJavaオブジェクトをjson文字列にシリアル化してStringEntityに生成されたjson文字列を追加します。

+0

を使用しています。{"message": "このエンドポイントではサポートされていないメソッドです"、 "code": "method_not_allowed"}}上記のコードの変更をお勧めします。 StringEntity input = new StringEntity( "{\"ステータス\ ":\"受け入れ\ "}"); input.setContentType( "application/json"); httpput.setEntity(入力); –

+0

@RohanChavanそれでは、HttpPutはHttpPostを試してみることはできません。 – Bhargav

+0

ありがとうございます –

関連する問題