Android使用HTTP-GET调用PHP

时间:2013-03-10 13:49:59

标签: php android http url http-post

我的问题是:如何使用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");

这是对的吗?

1 个答案:

答案 0 :(得分: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/