【问题标题】:Retrieve data from android app to PHP website将数据从 android 应用程序检索到 PHP 网站
【发布时间】:2015-08-05 07:19:03
【问题描述】:

是否有任何方法可以将数据从 Android 应用程序获取到网站?

如果是从网站到网站,使用“file_get_contents”是可能的。但是,对于从 Android 应用程序获取数据有什么想法吗?

提前致谢!

【问题讨论】:

    标签: php android


    【解决方案1】:

    使用这个简单的 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

    【讨论】:

    • 您的意思是您可能无法访问应用程序的源代码?你想做一个从安卓系统获取数据的网站吗?
    • 部分是的,从应用程序获取或了解最新消息。
    【解决方案2】:

    您可以在 android 中 execute post requests 访问您的网站。虽然这必须从不同的线程执行,因为 android 不允许 network operations on the main thread.

    【讨论】:

    • 这里是安卓应用,任何安卓应用。我可能无法访问该应用程序的源代码。就像我说的,在“file_get_contents”中我可以提供任何网址。同样的方法,有什么通用的方法吗?
    猜你喜欢
    • 2012-05-25
    • 1970-01-01
    • 1970-01-01
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多