java - 为什么我会收到此“E/org.json.JSONException:在字符 0 处输入结束:onErrorResponse:”错误?
问题描述
我正在使用从 android 设备到 spring 后端的 volley 库向服务器发送一个发布请求。此发布请求用于注册新用户。一切都按预期正常工作,将发布请求发送到服务器,服务器接受请求并在数据库上创建新用户。但是我没有成功创建用户 toast,而是在我的 android 应用程序中创建用户时出错。
我找不到代码中的内容。
这是我在 android 上的代码:
// sends data to the server
public void createNewUser(){
final String BASE_URL = BaseUrl.BASE_URL_CUSTOMER;
RequestQueue requestQueue = Volley.newRequestQueue(this);
// sending the json file to the server
JSONObject postParams = new JSONObject();
try {
postParams.put("firstName", customer.getFirstName());
postParams.put("lastName",customer.getLastName());
postParams.put("email",customer.getEmail());
postParams.put("password",customer.getPassword());
Log.e("testing", "createNewUser: "+postParams.toString());
}catch (Exception e){
Log.d(e.getMessage(), "createNewUser: error while creating a new user");
}
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, BASE_URL, postParams, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.e(response.toString(), "onResponse: " );
Toast.makeText(getApplicationContext(),"Creating new user",Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),"Error:Creating new user",Toast.LENGTH_SHORT).show();
Log.e(error.getMessage(), "onErrorResponse:");
}
});
requestQueue.add(jsonObjectRequest);
}
我检查了邮递员,我的回复如下所示:
{
"firstName": "xyz",
"lastName": "abc",
"email": "xyzabc@gmail.com",
"password": "12345678",
"_links": {
"self": {
"href": "http://localhost:8080/api/customers/22"
},
"customer": {
"href": "http://localhost:8080/api/customers/22"
}
}
}
这是我的全部错误:
com.android.volley.ParseError: org.json.JSONException: End of input at character 0 of
at com.android.volley.toolbox.JsonObjectRequest.parseNetworkResponse(JsonObjectRequest.java:73)
at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:132)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:87)
Caused by: org.json.JSONException: End of input at character 0 of
at org.json.JSONTokener.syntaxError(JSONTokener.java:449)
at org.json.JSONTokener.nextValue(JSONTokener.java:97)
at org.json.JSONObject.<init>(JSONObject.java:159)
at org.json.JSONObject.<init>(JSONObject.java:176)
at com.android.volley.toolbox.JsonObjectRequest.parseNetworkResponse(JsonObjectRequest.java:68)
解决方案
推荐阅读
- flutter - 通过单击按钮在 Flutter 中停止 while 循环
- python - Python 方法(在哪里可以找到其他资源)
- html - 在父div中水平居中div?
- python-3.x - 如何使用 matplotlib 和 gridspec 将 pickle 文件中的轴添加到现有图形?
- python - 运行代码时出现UnboundLocalError,这根本没有意义
- angular - 出现错误 RangeError:尝试导入共享模块 IONIC 时超出了最大调用堆栈大小
- python-3.x - 为什么索引如此有效?
- javascript - 如何分发带有子模块的 es6 包
- css - 有没有办法减小 Divi style.css 和 style.dev.css 的大小?
- r - 如何在r中将多个选定的列名从整数转换为日期