0

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"}]

Stephane Landelle
  • 6,990
  • 2
  • 23
  • 29
RohanC
  • 301
  • 1
  • 3
  • 12
  • Are you able to reach the URL from a browser running on the Android device? – Matt O May 03 '14 at 13:40
  • change your url with this http://192.168.1.4/clientservertest/returnUploadedImages.php to localhost://192.168.1.4/clientservertest/returnUploadedImages.php – Amardeepvijay May 03 '14 at 13:48
  • @MattO,@Amardeep I doubt this is a URL issue since similar code is working for me in the same activity. See my edit for the working code. – RohanC May 03 '14 at 13:55

1 Answers1

2

Looks like your server is returning a JSON array, so you should try overriding the onSuccess(JSONArray array) method in your JsonHttpResponseHandler.

client.get("http://192.168.1.4/clientservertest/returnUploadedImages.php",
    new JsonHttpResponseHandler() {
        @Override
        public void onSuccess(JSONArray jsonArray) {
            Toast.makeText(getApplicationContext(), "Success!", Toast.LENGTH_LONG).show();
            Log.d("android", jsonArray.toString());
        }
        //etc...
    });
Matt O
  • 1,336
  • 2
  • 10
  • 19