【发布时间】:2015-08-05 07:19:03
【问题描述】:
是否有任何方法可以将数据从 Android 应用程序获取到网站?
如果是从网站到网站,使用“file_get_contents”是可能的。但是,对于从 Android 应用程序获取数据有什么想法吗?
提前致谢!
【问题讨论】:
是否有任何方法可以将数据从 Android 应用程序获取到网站?
如果是从网站到网站,使用“file_get_contents”是可能的。但是,对于从 Android 应用程序获取数据有什么想法吗?
提前致谢!
【问题讨论】:
使用这个简单的 php 脚本,您可以从 android 获取数据:
<?php
$filename="datatest.html";
file_put_contents($filename,$_POST["fname"]."<br />",FILE_APPEND);
file_put_contents($filename,$_POST["fphone"]."<br />",FILE_APPEND);
file_put_contents($filename,$_POST["femail"]."<br />",FILE_APPEND);
file_put_contents($filename,$_POST["fcomment"]."<br />",FILE_APPEND);
$msg=file_get_contents($filename);
echo $msg; ?>
在 android 中可以使用HttpPost 来完成这个操作:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example.com/mypage.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("fname", "jake"));
nameValuePairs.add(new BasicNameValuePair("fphone", "9999999"));
nameValuePairs.add(new BasicNameValuePair("femail", "xyz@live.com"));
nameValuePairs.add(new BasicNameValuePair("fcomment", "Help"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
用BasicNameValuePair的构造函数中要设置和发送的字段替换你的数据。
这里是实际的Source
【讨论】:
您可以在 android 中 execute post requests 访问您的网站。虽然这必须从不同的线程执行,因为 android 不允许 network operations on the main thread.
【讨论】: