2

I have hosted my API on an online server. When I am testing it using the Postman app for chrome , its returning me the right results (ie. the details of the user from the database) But the below Volley request is getting wrong results (ie. Required Parameters missing);

Screenshot of Postman Request

Postman Test Screenshot

Volley Request

private void loginUser(final String username, final String password){

        String URL_REGISTER = "http://www.h8pathak.orgfree.com/jobs/login.php";
        pDialog.setMessage("Logging in...");
        showDialog();


        StringRequest strReq = new StringRequest(Request.Method.POST,
                URL_REGISTER, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                hideDialog();
                try {
                    JSONObject jObj = new JSONObject(response);
                    int result = jObj.getInt("result");


                    if(result==1){

                        session.setLogin(true);

                        JSONObject jObj2 = jObj.getJSONObject("user");
                        Intent i = new Intent(LoginActivity.this, EmployerActivity.class);
                        i.putExtra("username", jObj2.getString("username"));
                        i.putExtra("email", jObj2.getString("email"));
                        startActivity(i);
                        finish();
                        Toast.makeText(LoginActivity.this, jObj.getString("message"), Toast.LENGTH_SHORT).show();
                    }

                    else{
                        Toast.makeText(LoginActivity.this, jObj.getString("message"),Toast.LENGTH_SHORT).show();

                    }
                }

                catch (JSONException e) {
                    e.printStackTrace();
                }

            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(LoginActivity.this, volleyError.getMessage(), Toast.LENGTH_SHORT).show();
                hideDialog();
            }
        }){

            @Override
            protected Map<String, String> getParams()  {

                Map<String, String> params = new HashMap<>();
                params.put("username", username);
                params.put("password", password);

                return params;
            }
        };

        AppController.getInstance().addToRequestQueue(strReq);

    }

The PHP Code to receive request params and return the appropriate response.

if(isset($_POST['username']) && isset($_POST['password']))  {

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

      //Get the details from the database and echo in a json response

}

else{

    $message = "Required parameters missing!";
    $response['result']=0;
    $response['message']=$message;

    echo json_encode($response);
}   

What could be the possible mistake?

h8pathak
  • 1,342
  • 3
  • 17
  • 29
  • Did you add the username and password to request as parameters ? And also instead of StringRequest you should use JSONObjectRequest for this response. – Abdullah Tellioglu Jun 27 '16 at 11:38
  • what is the result you receive??share your logcat – SaravInfern Jun 27 '16 at 11:44
  • what are the other data you have passed in the postman apart from the url itself ? – Sagar Nayak Jun 27 '16 at 11:53
  • Please provide a full description of the problem. What exactly is going wrong? Did you already read [this](http://stackoverflow.com/questions/25948191/send-post-request-using-volley-and-receive-in-php)? – artkoenig Jun 27 '16 at 11:53
  • @Artjom I have added more details to the question. – h8pathak Jun 27 '16 at 14:47
  • @SaravInfern I have mentioned the results now. – h8pathak Jun 27 '16 at 14:48
  • have you disabled the volley cache? it is enabled by default – Sarthak Mittal Jun 27 '16 at 14:50
  • I tried your API with postman as displayed in the screenshot and I get always `{"result":0,"message":"Required parameters missing!"}`. Please provide all settings you used in postman for a successful request. – artkoenig Jun 28 '16 at 11:38
  • @Artjom I just used the form-data in the body section and passed the username and password keys along with their values as shown in the screenshot. I didn't use any other setting along with this. – h8pathak Jun 29 '16 at 04:31

2 Answers2

3

Something is wrong with your server configuration: http://www.h8pathak... is not responding as expected, but http://h8pathak... works. The latter is used in your postman example, use it in you Android code and it will work there too.

artkoenig
  • 7,117
  • 2
  • 40
  • 61
0

WWW is the answer. This must be a bug. It goes to the URL and runs it but does not POST.

http://www.example.com/myphpfile.php is wrong.

http://example.com/myphpfile.php is right.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Rob
  • 1
  • 1