0

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

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

这是正确的吗?

4

1 回答 1

0

您需要向您的服务器发出 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;
  }
}

我还可以为此服务器异步调用推荐 AQuery 库:

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

于 2013-03-10T15:54:48.600 回答