我一直试图在Android Studio中使用凌空复制以下curl请求数小时并不断收到错误:
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 +&#34; rides&#34;);
以下是我的截击码。它总是导致400 Server响应,错误是com.android.volley.ServerError&#39;。请让我知道我做错了什么。谢谢!
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 :(得分:0)
看看JsonObjectRequest。
scanf()
答案 1 :(得分:0)
我不确定上述答案是否也有效,但我找到了解决问题的另一种方法。我不得不重写getBody()和getBodyContentType()函数而不是getParams(),以防其他人遇到此问题。