Volley.正在将JSONObject作为参数发送



我在谷歌上搜索过其他SO文章。但没有任何帮助。请帮我找出问题出在哪里。我所做的是我想从StringRequest更改为JsonObjectRequest

我得到这个错误:

com.android.volley.ParseError: org.json.JSONException: End of input at character 0 of 

我的代码

RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://"+employee.get_ip_address()+"/NextrackAndroid/authenticate.php";
Log.d("TAG", "URL : "+ url);
JSONObject obj = new JSONObject();
try {
obj.put("id", id.toString());
obj.put("deviceID", deviceID.toString());
Log.d("TAG", obj.toString());
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,url,obj,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("TAG", "onResponse : " + response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("TAG", "onErrorResponse : " + error.toString());
error.printStackTrace();
}
}){
/** Passing some request headers* */
@Override
public Map getHeaders() throws AuthFailureError {
HashMap headers = new HashMap();
headers.put("Content-Type", "application/json");
return headers;
}
};
queue.add(jsObjRequest);

com.android.volley.ParseError:org.json.JSONException:字符0处的输入结束

这不是你的次要问题。当您的响应不是JSONObject时,此问题仍然存在。所以您的代码无法处理它。因为您已将JsonObjectRequest作为响应处理程序。

为了克服这个问题。

  • 在Postman上查看其回复,或向Web服务开发人员询问
  • 您会发现Web服务的响应不是完美的JSON
  • 然后请Web服务开发人员修复此问题
  • 或者,如果服务器端的响应不是JSON,则将JsonObjectRequest更改为StringRequest

最新更新