【问题标题】:How Save Data Persistent From PHP to Android [closed]如何将数据持久保存从 PHP 到 Android [关闭]
【发布时间】:2011-04-23 02:55:39
【问题描述】:

我如何将数据从 PHP 持久保存到 android?

示例: 我希望 www.google.com 中的所有字符串都保存在参数文本字符串中,并且我使用它..

【问题讨论】:

    标签: android httpconnection persistent-data


    【解决方案1】:

    使用 HttpClient。然后,使用您的 InputStream,检查此链接:

    http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

    并将其写入您想要的文件。

    一个快速而肮脏的例子:

    // This goes inside a try...
    HttpClient htc = new DefaultHttpClient();
    HttpGet get = new HttpGet("http://www.google.com");
    HttpResponse htr = null;
    htr = htc.execute(get);
    InputStream is = htr.getEntity().getContent();
    File saveFile = new File(getApplicationContext().getFilesDir().getPath() + File.separatorChar + "datos.txt");
    FileOutputStream fos = new FileOutputStream(saveFile);
    // Validate if exists and so on...
    int c;
    while ((c = is.read()) != -1) 
    {
        fos.write(c);
    }
    // Catch, finally, close, etc...
    

    您可能还想缓冲您的读写代码。

    另一种方法是使用:

    InputStream is = URL("http://www.google.com").getContent();
    

    这取决于你。我喜欢 HttpClient,因为你可以处理更多的事情。

    【讨论】:

    • @VicentePlata 你能给我一个使用内部存储的例子吗?我认为最好的一个我必须使用 sharedpreference,但我不知道如何使用它:( 我曾尝试将它放在我的 httpconnection 类中,但它给了我错误。
    • 完成。请查看我编辑的答案。
    • @VicentePlata 我试过你的答案,我尝试先用 toast 显示细节 Toast.makeText(getApplicationContext(), c,Toast.LENGTH_SHORT).show(); 但没有任何反应。 :$
    • 然后会发生什么?数据应保存在应用程序的路径、“数据”文件夹和名为 datos.txt 的文件中。那个文件出现了吗?
    • @VicentePlata 什么也没发生,也没有文件出现。应该在哪里创建文件?在可绘制文件夹中?还是我必须先创建文件然后覆盖它?
    猜你喜欢
    • 1970-01-01
    • 2020-10-20
    • 1970-01-01
    • 2019-03-01
    • 2019-10-14
    • 1970-01-01
    • 2012-09-06
    • 1970-01-01
    • 2011-04-13
    相关资源
    最近更新 更多