2017-02-16 116 views
0

我一直在試圖複製使用小時凌空Android Studio中,下列curl請求,並不斷收到錯誤在POST體字段發送JSON字典:通過運行使用凌空

curl -X POST -H "Authorization: Bearer <access_token> " \ 
    -H "Content-Type: application/json" \ 
    -d '{"ride_type" : "lyft", "origin" : {"lat" : 34.305658, "lng" : -118.8893667 } }' \ 
    'https://api.lyft.com/v1/rides' 

的函數被調用:

runPost(lyftCodeURL +「rides」);

以下是我的齊射代碼。它始終導致400服務器響應,錯誤是'com.android.volley.ServerError'。請讓我知道我做錯了什麼。謝謝!

private void runPost(String uri) { 
     StringRequest stringRequest = new StringRequest(Request.Method.POST, uri, 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 
         JSONObject obj = null; 
         try { 
          String res = stripHtml(response); 
          obj = new JSONObject(res); 
          tv.setText("res" + res.toString()); 
         } catch (Exception e) { 
          tv2.setText(e.toString()); 
         } 
         if (obj != null) { 
          handleLyftInfos(obj, 1); 
         } 
        } 
       }, 
       new Response.ErrorListener() 
       { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         tv.setText("err" + error.networkResponse.statusCode + " mfg " + error.toString()); 
        } 
       } 
     ) { 
      @Override 
      protected Map<String, String> getParams() { 
       Map<String, String> params = new HashMap<String, String>(); 
       JSONArray jArrayInput=new JSONArray(); 
       JSONObject originObject = new JSONObject(); 
       try { 
        params.put("ride_type", "lyft"); 
        originObject.put("lat", "34.305658"); 
        originObject.put("lng", "-118.8893667"); 
        params.put("origin", originObject.toString()); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
       Log.d("Param:", params.toString()); 
       return params; 
      } 
      @Override 
      public Map<String, String> getHeaders() throws AuthFailureError { 
       Map<String,String> params = new HashMap<String,String>(); 
       params.put("Authorization","Bearer " + aKey); params.put("Content-Type","application/json"); 
       return params; 
      } 
     }; 
     queue.add(stringRequest); 

    } 
+0

另外,我敢肯定,錯誤在於getParams()方法,我嘗試在「origin」中傳遞。我不知道爲什麼,但lyft服務器不會識別參數的組織方式。 –

回答

0

看看JsonObjectRequest。

JSONObject request = new JSONObject(); 
request.put("a","1"); 
request.put("v","33"); 

JsonObjectRequest strReq = new JsonObjectRequest(Request.Method.POST, "<URL>", request, 
     new Response.Listener<JSONObject>() { 

      @Override 

      public void onResponse(JSONObject response) { 
      }, 
     new Response.ErrorListener() { 

      @Override 
      public void onErrorResponse(VolleyError error) { 
      } 
     }); 
+0

所以要用我需要的數據來做到這一點,我只需要做: 'JSONObject request = new JSONObject(); JSONObject originObject = new JSONObject(); originObject.put(「lat」,「34.305658」); originObject.put(「lng」,「-118.8893667」); request.put(「ride_type」,「lyft」); request.put(「origin」,originObject);' –

+0

是的,JsonObjectRequest適用於發送和接收(您還必須接收application/json)。它負責POST/PUT的內容類型和正文等。 –

0

我不知道,如果上面的回答也可以,但是我想通了另一個解決我的問題。我必須重寫getBody()和getBodyContentType()函數,而不是getParams(),以防其他人遇到此問題。