我的问题是我需要向服务器发送JSON,但是当我调用该方法时,总是会收到400错误。
我使用了邮递员,并且正确创建了JSON。我使用Volley库。我尝试使用JsonRequest
和StringRequest
,但什么也没有。每次都出现相同的错误。
JSONObject jsonParam = new JSONObject();
try {
jsonParam.put("text", s);
jsonParam.put("language", "es-ES");
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println(jsonParam.toString());
RequestQueue queue = Volley.newRequestQueue(Castellano.this);
StringRequest jsonObjectRequest = new StringRequest(Request.Method.GET,url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(Castellano.this, response.toString(), Toast.LENGTH_LONG).show();
System.out.println(response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Castellano.this, error.toString(), Toast.LENGTH_LONG).show();
System.out.println(error.toString());
NetworkResponse networkResponse = error.networkResponse;
if (networkResponse != null) {
Log.e("Volley", "Error. HTTP Status Code:" + networkResponse.statusCode);
}
if (error instanceof TimeoutError) {
Log.e("Volley", "TimeoutError");
} else if (error instanceof NoConnectionError) {
Log.e("Volley", "NoConnectionError");
} else if (error instanceof AuthFailureError) {
Log.e("Volley", "AuthFailureError");
} else if (error instanceof ServerError) {
Log.e("Volley", "ServerError");
} else if (error instanceof NetworkError) {
Log.e("Volley", "NetworkError");
} else if (error instanceof ParseError) {
Log.e("Volley", "ParseError");
}
}
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json;");
return headers;
}
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("text", "");
params.put("language", "");
return params;
}
};
queue.add(jsonObjectRequest);
I / SpeechService:API已完成。
E / Volley:[182] BasicNetwork.performRequest:[已编辑的网址]的意外响应代码400
I / System.out:com.android.volley.ClientError
E / Volley:错误。 HTTP状态码:400
E / Volley:ServerError