Volley:如何从字符串响应中提取JSONObject?有可能吗?

问题描述 投票:4回答:1

我花了几天的时间试图弄清楚凌空,最后设法找到了使用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);

        }
    }

}

我感觉是因为响应以String返回(我可能是错误的)。但是我尝试使用自定义请求gson,但是我使用post方法所做的任何操作均不起作用。 StringRequest是唯一实际返回内容的方法,但是现在,我无法从中提取数据。我想在会话中存储数据。

如果有人用我的Google搜寻并尝试了我能找到但无济于事的任何解决方案,那么如果有人能提供帮助,那将非常好。谢谢!

php android json android-volley
1个回答
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();              
                }

            }
© www.soinside.com 2019 - 2024. All rights reserved.