首页 > 解决方案 > 为什么我会收到此“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)

标签: javaandroidjsonjsonexception

解决方案


推荐阅读