Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Volley: How to extract JSONObject from String Response? Is it possible?

I've spent a few days trying to figure out volley & finally managed to find out ways to use the Method.POST. I'm using StringRequest to pass email_address parameter to my PHP. Now it's returning a JSON string:

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

However, I couldn't extract data using the normal getString() method. It's giving me "no value" error. Here's my code. Just an extract of it:

    // 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);

        }
    }

}

I have a feeling it's because the response is returned as String (I could be wrong). But I've tried using custom request, gson but whatever I did using post method is not working. The StringRequest is the only method that actually return something but now, I couldn't extract data from it. I want to store the data in session.

It'd be great if anyone could help as I have googled & tried whatever solutions I could find but to no avail. Thanks!

like image 879
sw2 Avatar asked Dec 05 '22 23:12

sw2


1 Answers

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

            }
like image 132
VikasGoyal Avatar answered Feb 26 '23 00:02

VikasGoyal