2017-02-16 152 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(),以防其他人遇到此问题。