I am building an Android App that queries my online server for data, log-in info being one of them.
I have this log-in function where I shoot a request to a php script I have.
public void login(final String username, final String password, Context context, final requestHandler listener) {
final String requestUrl = "http://www.my-url.com/folder/folder/folder/script.php";
JSONObject object = new JSONObject();
try {
// TODO: handle loggedInUser authentication
//input your API parameters
object.put("user_identifier ", username);
object.put("password", password);
} catch (Exception e) {
Log.e(TAG, "error logging in = " + e);
new Result.Error(new IOException("Error logging in", e));
}
HashMap<String, String> params = new HashMap<>();
params.put("user_identifier", username);
params.put("password", password);
params.put("Content-Type", "application/json");
StringRequest sr = new StringRequest(Request.Method.POST, requestUrl,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("HttpClient", "success! response: " + response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("HttpClient", "error: " + error.toString());
}
})
{
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<>();
params.put("user_identifier", username);
params.put("password", "hello_World");
return params;
}
};
JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, requestUrl, new JSONObject(params),
new Response.Listener<JSONObject>() {
@Override public void onResponse(JSONObject response) {
//the response contains the result from the server
// a json string or any other object returned by your server
Log.e(TAG, "Response: " + response.toString());
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setDateFormat("yyyy/MM/dd hh:mm a");
gson = gsonBuilder.create();
ServerResponse serverResponse = gson.fromJson(response.toString(), ServerResponse.class);
Result<UserModel> result;
if(serverResponse.isSuccess()){
UserModel userModel = gson.fromJson(serverResponse.getData(), UserModel.class);
result = new Result.Success<>(userModel);
listener.passResult(result);
}
else {
result = new Result.Error(new IOException("Error code: " + serverResponse.getMessage()));
}
listener.passResult(result);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "volley error jsonObject = " + error);
error.printStackTrace(); //log the error resulting from the request for diagnosis/debugging
}
});
Volley.newRequestQueue(context).add(jsonRequest);
Volley.newRequestQueue(context).add(sr);
}
Now, the PHP will just get the request and then processes it. It returns a JSONObject, which is then parsed by the Android App I made. Kindly note that I was able to make requests before. But suddenly, I'm getting a blank response, which led to errors in parsing the JSONObject result. The StringRequest is also returning a string with 0 length.
Clearly, something is wrong. I have used apirequest.io to test my PHP script, and my script is able to receive, process, and output the right results.
Clearly, there is something wrong with my Android. To make sure that my PHP is running well, I have this block, before I process in the input:
print_r($_REQUEST);
var_dump($_REQUEST);
print_r($_POST);
var_dump($_POST);
print_r($_GET);
var_dump($_GET);
But when I run the log-in function again, I am getting nothing from my server. Dumping the $_REQUEST
parameter just gives a blank. It might be useful to note that the PHP script still functions as intended when I use apirequest.io. There is clearly something odd with how Android Volley builds my requests and sends them to the server.
It is not until I hardcode the value in the url, something like: requestUrl?name_1=value1&name2_value2..
that the PHP shows it was able to receive parameters in the $_GET
Request.
This is mind boggling.
I am now stumped, it seems that Volley, out of the blue, just stopped constructing my request properly, hence, my parameters being dropped.
Can someone point out what I'm doing wrong?