I've just started using the LoopJ AndroidAsyncHttp Library and its brilliant for data uploading.
However, I'm now trying to get a response using a get request and I can't seem to understand why neither my onSuccess nor my onFailure methods are being called. I've looked through the questions here and can't seem to find one that addresses the new implementations of the onSuccess methods. Could someone please help?
Method being called on ButtonClick:
public void displayUploaded(View view){
RequestParams params=new RequestParams();
try{
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://192.168.1.4/clientservertest/returnUploadedImages.php",
new JsonHttpResponseHandler() {
@Override
public void onSuccess(JSONObject jsonObject) {
// Display a "Toast" message
Toast.makeText(getApplicationContext(), "Success!", Toast.LENGTH_LONG).show();
Log.d("android", jsonObject.toString());
}
@Override
public void onFailure(int statusCode, Throwable throwable, JSONObject error) {
// Display a "Toast" message
Toast.makeText(getApplicationContext(), "Error: " + statusCode + " " + throwable.getMessage(), Toast.LENGTH_LONG).show();
// Log error message
// to help solve any problems
Log.e("android", statusCode + " " + throwable.getMessage());
}
});
}
catch(Exception e){
Toast toast2=Toast.makeText(getApplicationContext(),"Failed first TRY",Toast.LENGTH_LONG);
toast2.show();
e.printStackTrace();
}
}
This is my php code (Which works fine using Postman Client on chrome, I've posted the output in EDIT2):
<?php
#Connect to Database
$con = mysqli_connect("localhost","root","", "mytestdatabase");
#Check connection
if (mysqli_connect_errno()) {
echo 'Database connection error: ' . mysqli_connect_error();
exit();
}
#Query the database to get the user details.
$userdetails = mysqli_query($con, "SELECT * FROM images");
#If no data was returned, check for any SQL errors
if (!$userdetails) {
echo 'Could not run query: ' . mysqli_error($con);
exit;
}
#Return the results
$rows = array();
while($r = mysqli_fetch_assoc($userdetails)) {
$rows[] = $r;
}
print(json_encode($rows));
?>
I've also tried the same thing using the other ResponseHandlers but that doesn't work either. Really hoping for an answer!
EDIT: Adding code which is valid and working perfectly in the same activity. This is a post request:
File selectedPicture=new File(picturePath);
RequestParams params=new RequestParams();
try{
params.put("UploadedPic",selectedPicture);
AsyncHttpClient client = new AsyncHttpClient();
client.post("http://192.168.1.4/clientservertest/imageupload.php", params,new AsyncHttpResponseHandler());
}
EDIT2: Response returned to the POSTMAN client from the php page:
[{"id":"7","path":"uploads/speed.png"},{"id":"8","path":"uploads/Untitled.png"},{"id":"9","path":"uploads/Untitled.png"},{"id":"10","path":"uploadsspeed_2.png"},{"id":"11","path":"uploads/speed_3.png"}]