【发布时间】:2013-03-10 13:49:59
【问题描述】:
我的问题是:如何使用 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");
这样对吗?
【问题讨论】:
-
"counting +1"..你能进一步解释一下吗?为什么不制作另一个 PHP 脚本,例如“example.de/api.php”?
-
@redreggae 我们的 PHP 负责统计浏览量。因此,如果您访问 www.example.com/test.php,PHP 会在 TextFile 中写入查看次数。
-
好的,但是你为什么不为 Android 创建另一个入口点,比如“example.de/api.php”,但它不算数?你不能在另一台机器的命令行上运行 PHP。
-
所以我想要的只是调用这个 URL:www.example.com/test.php 但不在 WebView 中......如何做到这一点?
-
@redreggae 你的意思是什么:你不能在另一台机器的命令行上运行 PHP?
标签: php android http url http-post