Android使用HTTP-GET调用PHP


Android call PHP with HTTP-GET

我的问题是:如何调用php与HttpPost?

 final HttpClient httpclient = new DefaultHttpClient();
final HttpPost httppost = new HttpPost("www.example.de/mySkript.php");
final ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("param1", "value1"));
nameValuePairs.add(new BasicNameValuePair("param2", "value2"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
final HttpResponse response = httpclient.execute(httppost);

我们发现这个…但是我们不想把参数/值发送给PHP,因为如果你通过URL调用它,我们的PHP会计算+1。有调用PHP的代码吗?

谢谢你:)

编辑:PHP是:

<?php
$userdatei = fopen ("test.txt","r");
$zeile = fgets($userdatei, 500);
$zeile++;
fclose($userdatei);
$schreiben = fopen ("test.txt","w");
fwrite($schreiben, $zeile);
fclose($schreiben);
?>

并使用此代码:

    public static HttpResponse hitUrl(String url) {
      try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = httpclient.execute(new HttpGet(url));
        return response;
      } catch (Exception e) {
          e.printStackTrace();
        return null;
      }
    }

并使用:

hitUrl("http://example.de/test.php");

这样对吗?

您需要向服务器发出HTTP GET请求,您可以使用以下内容:

public static HttpResponse hitUrl(String url) {
  try {
    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response = httpclient.execute(new HttpGet(url));
    return response;
  } catch (Exception e) {
    Log.("[GET REQUEST]", "Network exception", e);
    return null;
  }
}

我还可以为这个服务器异步调用推荐一个query库:

http://code.google.com/p/android-query/