Android call PHP with HTTP-GET

偶尔善良 提交于 2019-12-25 06:02:03

问题


My question is: How to call a php with 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);

We found this...but we don't want to send Parameters/Values to the PHP because our PHP is counting +1 if you call it through the URL. Any code for just call the PHP?

Thank you :)

edit: The PHP is:

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

$schreiben = fopen ("test.txt","w");
fwrite($schreiben, $zeile);
fclose($schreiben);
?>

And Use this Code:

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

And call it with:

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

Is this right?


回答1:


You need to make HTTP GET request to your server, you can use something like:

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

I can also recommend AQuery library for this server asynchronous calls:

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



来源:https://stackoverflow.com/questions/15322940/android-call-php-with-http-get

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!