jeudi 13 août 2015

POST String From Android To Php

I'm having trouble posting a String from android to php. I'm trying to post a username since I was to exclude the current users username from the query. In Java I have an AsyncTask with the code below in the doInBackground(String... params)

ArrayList<NameValuePair> data_to_send = new ArrayList<>();
    data_to_send.add(new BasicNameValuePair("Username", username));

    HttpParams httpRequestParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpRequestParams, ServerRequests.CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(httpRequestParams, ServerRequests.CONNECTION_TIMEOUT);


    HttpClient client = new DefaultHttpClient(httpRequestParams);
    HttpPost post = new HttpPost(ServerRequests.SERVER_ADDRESS + "TestQuery.php");

    try {
        post.setEntity(new UrlEncodedFormEntity(data_to_send));
        client.execute(post);
    } catch (Exception e) {

        e.printStackTrace();
    }

And this is in my php:

$origLat = 45.6215349;
$origLon = 18.6951613;
$dist = 30; 
$username = $_POST["Username"];

$sql = "SELECT ID, Username, Name, Age, City, Gender, Latitude, Longitude, 3956 * 2 * 1.609344 * 1000 *
      ASIN(SQRT( POWER(SIN(($origLat - abs(Latitude))*pi()/180/2),2)
      +COS($origLat*pi()/180 )*COS(abs(Latitude)*pi()/180)
      *POWER(SIN(($origLon-Longitude)*pi()/180/2),2))) 
      as distance FROM users WHERE Username != '$username'
      AND Longitude BETWEEN ($origLon-$dist/abs(cos(radians($origLat))*69)) 
      AND ($origLon+$dist/abs(cos(radians($origLat))*69)) 
      AND Latitude BETWEEN ($origLat-($dist/69)) 
      AND ($origLat+($dist/69))
      HAVING distance < $dist ORDER BY distance limit 30;"; 

Well the query works when I input the String directly by changing the Username != 'Jawe' but I'm not sure why it doesn't work when posted from Android. Anyone know the problem?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire