Deepesh Tiwari
Deepesh Tiwari

Reputation: 49

Volley string request post parameters always null in php.

I am sending a string POST request with some parameters but in PHP script i'm always getting parameters value as null. When i test the PHP with postman its working fine.

this is my volley request code:

public void login(final String mPhone, final String mEmail, final String mPassword) {


        StringRequest stringRequest = new StringRequest(Request.Method.POST, URLs.LOGIN, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject.getBoolean("error")) {
                        String error = jsonObject.getString("message");
                        if (error.equals("new user")) {
                            registerUser();
                        } else {
                            showProgress(false);
                            Snackbar.make(mOtpView, error, Snackbar.LENGTH_SHORT).show();
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                showProgress(false);
                Snackbar.make(mOtpView, error.getMessage(), Snackbar.LENGTH_SHORT).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<>();
                params.put("phone", mPhone);
                params.put("email", mEmail);
                params.put("password", mPassword);

                return params;
            }
        };
        //Method to limit retry policy of request
        stringRequest.setRetryPolicy(
                new DefaultRetryPolicy(
                        DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2,
                        3,
                        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
                )
        );
        try {

            //Adding request to request queue
            RequestQueue requestQueue = Volley.newRequestQueue(this);
            requestQueue.add(stringRequest);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

and this is my PHP:

require_once 'DbConfig.php';

$response = array();

if (isset($_GET['apicall'])) {
    switch ($_GET['apicall']) {
        case 'login':

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

            echo $phone.$email.$pass;exit();

i have tried adding header by overriding getHeaders() and also tried overriding getBodyContent() but nothing seems to work and also the above code is working fine when im using it in localhost. i checked the URL its fine because the PHP script is fine as i have tested it by echoing other things it executed fine.

Upvotes: 0

Views: 847

Answers (3)

Deepesh Tiwari
Deepesh Tiwari

Reputation: 49

SOLVED!!! Cant think about this ever!! actually i was using URL like:- http://www.example.com but when i tried http://example.com .. it worked as charm... thanx everyone for their help though!!

Upvotes: 2

ElectronSz
ElectronSz

Reputation: 32

You are only sending ;

params.put("phone", mPhone); params.put("email", mEmail); params.put("password", mPassword);

How about the; params.put("apicall","YOu_API");

I guess apicall is not isset, so the values will be null.

Upvotes: 0

HamzaNig
HamzaNig

Reputation: 1029

Usauly this problem could be from Content-Type add charset=utf-8 to it :

 params.put("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");

Credit : Volley-POST-EMPTY

Upvotes: 0

Related Questions