【发布时间】:2017-01-13 12:56:41
【问题描述】:
我是 Android 的初学者。我正在尝试使用 volley 在 Android 中连接休息网络服务。它在邮递员(chrome 扩展)中工作正常,但它没有得到正确的 JSON 对象响应(我找到了这样的字符串响应)
"<!doctype html... "
但是返回状态码是200。method是POST方法,body是
{ "email":"mail.xxxxxxxx@gmail.com","password":"123456" }
Content-Type application/json,
需要的输出
{"success":1,"data":{"customer_id":"358","name":"xxx..","email":"mail.xxxxxx@gmail.com"},"message":"xxxxx"}
我正在尝试两种方法
1)
try {
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
String URL = "http://www.xxxxx";
JSONObject jsonBody = new JSONObject();
jsonBody.put("email", "mail.xxxxxx@gmail.com");
jsonBody.put("password", "123456");
final String requestBody = jsonBody.toString();
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new
Response.Listener<String() {
@Override
public void onResponse(String response) {
Toast.makeText(MainActivity.this, String.valueOf(response), Toast.LENGTH_SHORT).show();
Log.i("VOLLEY", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//Toast.makeText(MainActivity.this,String.valueOf(response),Toast.LENGTH_SHORT).show();
Log.e("VOLLEY", error.toString());
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public byte[] getBody() throws AuthFailureError {
try {
return requestBody == null ? null : requestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
return null;
}
}
@Override
protected Response<String parseNetworkResponse(NetworkResponse response) {
String responseString = "";
if (response != null) {
Log.i("VOLLEY RESPONSE", String.valueOf(response));
responseString = String.valueOf(response.statusCode);
// can get more details such as response.headers
}
return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
}
};
requestQueue.add(stringRequest);
} catch (JSONException e) {
e.printStackTrace();
}
in this method i found jsonObject parsing error,
2)
try {
RequestQueue requestQueue =
Volley.newRequestQueue(MainActivity.this);
String URL = "http://www.xxxxx";
JSONObject jsonBody = new JSONObject();
jsonBody.put("email", "mail.xxxxx@gmail.com");
jsonBody.put("password", "123456");
final String requestBody = jsonBody.toString();
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String() {
@Override
public void onResponse(String response) {
Toast.makeText(MainActivity.this,String.valueOf(response),Toast.LENGTH_SHORT).show();
Log.e("VOLLEY OUTPUT NEW",String.valueOf(response));**strong text**
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this,String.valueOf(error),Toast.LENGTH_SHORT).show();
}
}){
@Override
public byte[] getBody() throws AuthFailureError {
try {
return requestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
};
requestQueue.add(stringRequest);
} catch (JSONException e) {
e.printStackTrace();
}
在这里我找到了 200 状态码,但响应不是必需的。
【问题讨论】:
-
你能发布你的api代码吗?
-
因为您希望 JSON 返回,您可以使用 JsonObjectRequest 和 Response.Listener
作为 responseListener,这样可能更容易发现问题
标签: javascript android json rest