Volley:如何从String Response中提取JSONObject?可能吗?

sw2*_*sw2 4 php android json android-volley

我花了几天时间试图找出凌空并最终设法找到使用Method.POST的方法.我正在使用StringRequest将email_address参数传递给我的PHP.现在它返回一个JSON字符串:

    {"user_id":1,"email_address":"adminuser"}
Run Code Online (Sandbox Code Playgroud)

但是,我无法使用普通的getString()方法提取数据.它给了我"没有价值"的错误.这是我的代码.只是它的摘录:

    // Login button
    btnLogin = (Button) findViewById(R.id.btnLogin);


    // Login button click event
    btnLogin.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String param = txtUsername.getText().toString();
            postData(param);
        }
    });     

} 

/**
 * Posting parameter
 * */
private void postData(final String param) {

    // Get username, password from EditText
    String username = txtUsername.getText().toString();
    String password = txtPassword.getText().toString();

    // Check if username, password is filled
    if(username.trim().length() > 0 && password.trim().length() > 0){
        if(username.equals("adminuser") && password.equals("test")){
            RequestQueue rq = Volley.newRequestQueue(this);

            StringRequest postReq = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {
                    Log.d(TAG, response.toString());

                    try {

                        JSONObject person = new JSONObject();
                        String name = person.getString("user_id");
                        String email = person.getString("email_address");

                        //Store session
                        session.createLoginSession(name, email);

                        // Staring MainActivity
                        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                        startActivity(intent);
                        finish();
                        //}

                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();              
                    }

                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();


                }

            })  {

                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("email_address", param);
                    return params;
                }


            };

            rq.add(postReq);

        }
    }

}
Run Code Online (Sandbox Code Playgroud)

我有一种感觉,因为响应以字符串形式返回(我可能是错的).但我尝试使用自定义请求,gson,但无论我使用post方法做什么都无法正常工作.StringRequest是实际返回的唯一方法,但现在,我无法从中提取数据.我想将数据存储在会话中.

如果有人可以提供帮助,那就太好了,因为我用Google搜索并尝试了我能找到的任何解决方案,但无济于事.谢谢!

Avi*_*Avi 12

            @Override
            public void onResponse(String response) {
                Log.d(TAG, response.toString());

                try {
                    //Do it with this it will work
                    JSONObject person = new JSONObject(response);
                    String name = person.getString("user_id");
                    String email = person.getString("email_address");

                    //Store session
                    session.createLoginSession(name, email);

                    // Staring MainActivity
                    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                    startActivity(intent);
                    finish();
                    //}

                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();              
                }

            }
Run Code Online (Sandbox Code Playgroud)