0

Hi i am using php to connect my android app to mysql but the get method is not passing the value to the php file.

This is my code.

private void executeAjaxRequest(){

       String url = mDataUrl+"?request="+mRequest+"&outlet_id="+mOutletID;
       Log.v("url",url);
      System.out.println("This is StoreActivity " +url);

       AsyncHttpClient httpclient = new AsyncHttpClient();
       httpclient.get(url, new AsyncHttpResponseHandler() {
         @Override
         public void onSuccess(String response) {
            setOutletData(response);
            Log.i("TAG",response);
         }
       });
     }

It should pass the outletid and mrequest to the php file to get the data from the database.

Can you tell me where i am going wrong.

error_reporting(0);

    //$url = $_GET['url'];
    $mR = $_GET["mRequest"];
    $mOid = $_GET["mOutletID"];
    //$mloc = $_GET['mLocation'];
    //connect to the db
    //echo $mOid;
    $user = "root";
    $pswd = "";
    $db = "recommendations_db";
    $host = "localhost";
    $conn = mysql_connect($host, $user, $pswd);
    mysql_select_db($db);
    //if($mR == 'outlets' && $mloc = 'all'){
    $query = "SELECT outlet_id,outlet_name,outlet_location,outlet_image FROM outlets WHERE outlet_id = '$mOid'";
    $result = mysql_query($query) or die("Unable to execute query because : " . mysql_error());
    //echo $result ." ". $mOid;
    while($row = mysql_fetch_assoc($result))
    {
      $query2 = "SELECT a.item_id,a.item_name,a.item_image FROM items a,outlets b,recommendations c WHERE a.item_id = c.item_id AND b.outlet_id = c.outlet_id AND b.outlet_id = ".$row['outlet_id'];
       $row['recommended_products']=array();

        $result2 = mysql_query($query2) or die("Unable to execute query because : " . mysql_error());
        //echo $row;
        while($row2 = mysql_fetch_assoc($result2)){
            $row['recommended_products'][]=$row2;
            //echo $row;
        }

      $output[] = $row; 
    }
    print( json_encode($output) );
    mysql_close($conn);
morten.c
  • 3,414
  • 5
  • 40
  • 45

1 Answers1

0

Are you able to retrieve the request and outletid on your server ? If the request is indeed and SQL request it should be url encoded to avoid truncating the URL with some potentially "dangerous" characters.

You should override the method onFailure from AsyncHttpResponseHandler to handle any possible error. What you could also try is to use the RequestParams instead of putting the parameters and their values in the URL. You can use them like so:

    RequestParams params = new RequestParams();
    params.put("request", "mRequest");
    params.put("outlet_id", "mOutletID");
    AsyncHttpClient client = new AsyncHttpClient();
    client.get("URL", params, new AsyncHttpResponseHandler() {

        @Override
        public void onFailure(Throwable arg0, String arg1) {
            // Handle the error
        }

        @Override
        public void onSuccess(String arg0) {
            // Do stuff with the result
        }
    });
El Bert
  • 2,958
  • 1
  • 28
  • 36