Question

I'm new to Android Volley Library. I'm sending username and password to the API and API returns the user details. so here's my LoginUser method in android.

public void LoginUser(){

    // Tag used to cancel the request
    String tag_json_obj = "json_obj_req";

    String url = "http://example.com/myfile/mobile_api/";

    //final ProgressDialog pDialog = ProgressDialog.show(getParent(), "Please wait", "Login user");


            JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
                    url, null,
                    new Response.Listener<JSONObject>() {

                        @Override
                        public void onResponse(JSONObject response) {
                            try {
                                if(response.getString(KEY_SUCCESS)!=null){

                                    String res = response.getString(KEY_SUCCESS);

                                    if(Integer.parseInt(res) == 1){
                                       JSONObject json_user = response.getJSONObject("user");
                                       Toast.makeText(getApplicationContext(), "User Logged in.. " + json_user.getString("name"), Toast.LENGTH_LONG).show();
                                    }
                                }
                                else if(response.getString(KEY_ERROR)!=null){

                                    String res = response.getString(KEY_ERROR);

                                    if(Integer.parseInt(res) == 1){
                                        Toast.makeText(getApplicationContext(), response.getString("error_msg"), Toast.LENGTH_LONG).show();
                                    }
                                }

                            } catch (JSONException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                                //pDialog.hide();
                            }
                            Log.d(TAG, response.toString());
                            //pDialog.hide();
                        }
                    }, new Response.ErrorListener() {

                        @Override
                        public void onErrorResponse(VolleyError error) {
                            VolleyLog.d(TAG, "Error: " + error.getMessage());
                            Log.d(TAG, "Error: " + error.getMessage());
                            //pDialog.hide();
                        }
                    }) {

                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("tag", login_tag);
                    params.put("email", Email);
                    params.put("password", Pass);

                    return params;
                }

            };

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
}

and this is my API index.php file

if ($tag == 'login') {

    $email = $_POST['email'];
    $password = $_POST['password'];


    $user = $db->getUserByEmailAndPassword($email, $password);
    if ($user != false) {

        $response["success"] = 1;
        $response["uid"] = $user["id"];
        $response["user"]["name"] = $user["name"];
        $response["user"]["phone"] = $user["contact_no"];
        $response["user"]["email"] = $user["email"];
        $response["user"]["reg_date"] = $user["reg_date"];
        echo json_encode($response);
    } else {

        $response["error"] = 1;
        $response["error_msg"] = "Incorrect email or password!";
        echo json_encode($response);
    }

But I'm getting following error from Json

07-31 16:58:27.307: E/JSON Parser(7506): Error parsing data org.json.JSONException: Value No of type java.lang.String cannot be converted to JSONObject

So is this the correct way of connecting mobile API and the Android App using Volley? or can anyone explain me the reason for this?

Was it helpful?

Solution

Well, I think I've found the problem. i'm getting that error message because it returns a string "Access Denied" if the tag is incorrect it gives the "Access Denied" message. so this not a json object it's just a string. To solve the error you can use String Request to get the string value.

StringRequest strReq = new StringRequest(Method.POST, //here change the method to POST
            Const.URL_STRING_REQ, new Response.Listener<String>() {

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

                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    hideProgressDialog();
                }


            }){

        @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("tag", login_tag);
                params.put("email", Email);
                params.put("password", Pass);

                return params;
            }
    };
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top