【问题标题】:Volley: How to extract JSONObject from String Response? Is it possible?Volley:如何从字符串响应中提取 JSONObject?可能吗?
【发布时间】:2015-02-27 07:22:05
【问题描述】:

我花了几天时间试图弄清楚 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 是唯一实际返回某些内容的方法,但现在,我无法从中提取数据。我想将数据存储在会话中。

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

【问题讨论】:

    标签: php android json android-volley


    【解决方案1】:
                @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();              
                    }
    
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-24
      • 1970-01-01
      • 2018-06-11
      • 1970-01-01
      • 2014-08-30
      相关资源
      最近更新 更多