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);
4

1 回答 1

0

你能在你的服务器上检索requestand吗?outletid如果请求确实是 SQL 请求,则应该对它进行 url 编码,以避免使用一些潜在的“危险”字符截断 URL。

您应该覆盖方法onFailurefrom AsyncHttpResponseHandler以处理任何可能的错误。您还可以尝试使用RequestParams而不是将参数及其值放在 URL 中。你可以像这样使用它们:

    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
        }
    });
于 2013-03-14T17:32:43.470 回答