onResponse方法不起作用

时间:2017-06-19 22:09:39

标签: android spring-mvc android-volley

我有一个奇怪的问题,我的Volley中的onResponse方法没有被调用。控制器中的所有内容都可以,数据保存到数据库但是响应,我什么也没得到。我总是得到“连接问题”而不是......

JSONObject newUser = new JSONObject();
        try {
            newUser.put("login", login.toString());
            newUser.put("password", password.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        System.out.println("USER TO SAVE: " + newUser.toString());
        JsonObjectRequest urlAccStringRequest = new JsonObjectRequest(Request.Method.POST, urlAcc, newUser, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                System.out.println("Good morning " + login.toString());
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                progressDialog.dismiss();
                Toast.makeText(RegisterIndividualActivity.this, "Connection error", Toast.LENGTH_SHORT).show(); //this is what i got
            }
        });

这是控制器(我的意思是,这只是一个大控制器类的方法),它可以正常工作,数据被保存,所有内容都打印在屏幕上:

@RequestMapping(value = "/register", method = RequestMethod.POST)
    public void register(
            @RequestParam("login") String login,
            @RequestParam("password") String password,
            HttpServletRequest request, HttpServletResponse response, Model model
    ){
        System.out.println("\nIn REGISTER controller...");
        UserModel uM = new UserModel();
        HttpSession session = request.getSession(true);
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");

        uM.setAccountType("Client");


        try {
            System.out.println("\nTrying to save...");
            BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
            String json = "";
            String line;
            while((line=reader.readLine())!=null) json += line;
            JSONObject jsonObject = new JSONObject(json);
            System.out.println(jsonObject);
            uM.setLogin(jsonObject.getString("login"));
            uM.setPassword(jsonObject.getString("password"));
            userDAO.save(uM);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

@EDIT 添加了我发现的内容(感谢评论)

W/System.err: com.b.a.k: org.json.JSONException: End of input at character 0 of 
W/System.err:     at com.b.a.a.i.a(Unknown Source)
W/System.err:     at com.b.a.g.run(Unknown Source)
W/System.err: Caused by: org.json.JSONException: End of input at character 0 of 
W/System.err:     at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
W/System.err:     at org.json.JSONTokener.nextValue(JSONTokener.java:97)
W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:156)
W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:173)
W/System.err:   ... 2 more

0 个答案:

没有答案