Reputation: 1382
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
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?
Upvotes: 2
Views: 853
Reputation: 1
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.
Upvotes: 0
Reputation: 7257
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.
Upvotes: 3