Volley: 如何从字符串响应中提取JSONObject?这是否可能?

4

我花了几天时间来研究Volley,最终成功找到了使用Method.POST的方法。我正在使用StringRequest将email_address参数传递给我的PHP。现在它返回一个JSON字符串:

    {"user_id":1,"email_address":"adminuser"}

然而,我无法使用常规的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);

        }
    }

}

我有一种感觉是因为响应以字符串形式返回(我可能错了)。但是,无论我使用自定义请求、gson还是任何使用post方法的方法都不起作用。StringRequest是唯一能够实际返回数据的方法,但现在,我无法从中提取数据。我想将数据存储在会话中。
如果有人能够帮忙,那就太好了,因为我已经尝试过谷歌和所有我能找到的解决方案,但都没有成功。谢谢!
1个回答

15
            @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();              
                }

            }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接