POST字符串从Android到PHP

时间:2015-08-14 00:43:11

标签: php android apache http post

我无法将Android中的字符串发布到PHP。我正在尝试发布用户名,因为我要从查询中排除当前用户的用户名。在Java中,我在doInBackground(String参数)中有一个带有以下代码的AsyncTask。

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();
    }

这是我的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;"; 

当我通过更改Username != 'Jawe'直接输入字符串时查询有效,但我不确定为什么它从Android发布时不起作用。有人知道这个问题吗?

0 个答案:

没有答案