android volley上的字符串请求返回null

时间:2016-04-24 19:57:44

标签: php android json rest android-volley

抱歉,如果我对Android编程有点缺乏经验,如果问题很愚蠢,但我需要在android上使用volley的帮助,我已经在PHP上注册了一个使用REST API的用户,它的工作正常用phpunit和postman测试。这是代码:

public void newUser(final String name, final String email, final String password){
    String tag_string_req = "req_register";

    pDialog.setMessage("Registrando...");
    pDialog.show();

    StringRequest strReq = new StringRequest(Request.Method.POST, AppConfig.url_register,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.d(TAG, "Register response: " + response.toString());
                    pDialog.hide();

                    try {
                        JSONObject jObj = new JSONObject(response);
                        boolean error = jObj.getBoolean("error");
                        if (!error) {
                            String uid = jObj.getString("uid");

                            JSONObject user = jObj.getJSONObject("user");
                            String name = user.getString("name");
                            String email = user.getString("email");

                            db.addUser(name, email, uid);

                            Toast.makeText(getApplicationContext(), name + "Hurray!, registrado tente logar agora ^^", Toast.LENGTH_SHORT).show();

                            Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
                            startActivity(intent);
                            finish();


                        } else {
                            String errorMsg = jObj.getString("error_msg");
                            Toast.makeText(getApplicationContext(), errorMsg, Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Erro no registro: " + error.getMessage());
            Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }){
        @Override
        protected Map<String, String> getParams(){
            HashMap<String, String> params = new HashMap<>();
            params.put("name", name);
            params.put("email", email);
            params.put("password", password);
            return params;
        }
    };
        MySingleton.getInstance().addToRequestQueue(strReq);
    }

所以看起来像params或strReq返回null,我尝试了一个带有JSON的自定义请求我能找到的一切,我在这里读到StackOverflow,getParams()不应该存在或覆盖方法getBody() ,更改并继续返回null,异常java.lang.NullPointerException,谢谢。

0 个答案:

没有答案