JsonObjectRequest
,我收到了错误消息。帮助我......
Map<String, String> jsonParams = new HashMap<String, String>();
jsonParams.put("email",email.getText().toString());
JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, json_url, new JSONObject(jsonParams), new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Log.d("Response", response.toString());
name.setText(response.getString("Name"));
mobile.setText(response.getString("Mobile"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError{
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
MySingleton.getInstance(getApplicationContext()).addToRequestQueue(jsonObjectRequest);
答案 0 :(得分:0)
尝试使用volley StringRequest ,似乎服务器正在向您发送字符串而不是JsonOBJECTS。如果您需要JsonObject,则必须在服务器端进行一些更改。如果没有试试这个:
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
StringRequest request = new StringRequest(Request.Method.POST,
Addresses.LOGIN_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
Log.d("Response", jsonObject .toString());
name.setText(jsonObject .getString("Name"));
mobile.setText(jsonObject .getString("Mobile"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
queue.add(request);
答案 1 :(得分:0)
如果您的服务器以json格式接收值,这可能是您的解决方案。
JSONObject jsonobject= new JSONObject();
jsonobject.put("email",email.getText().toString());
JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, json_url, jsonobject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Log.d("Response", response.toString());
name.setText(response.getString("Name"));
mobile.setText(response.getString("Mobile"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
MySingleton.getInstance(getApplicationContext()).addToRequestQueue(jsonObjectRequest);