如何将数据从PHP持久保存到Android

如何将数据从PHP持久保存到Android,android,httpconnection,persistent-data,Android,Httpconnection,Persistent Data,如何将数据从PHP持久保存到android 例如: 我希望www.google.com中的所有字符串都保存在参数textstring中,并使用它。使用HttpClient。然后,使用InputStream检查以下链接: 并将其写入所需的文件 一个快速而肮脏的例子: // This goes inside a try... HttpClient htc = new DefaultHttpClient(); HttpGet get = new HttpGet("http://www.google.

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

例如:
我希望www.google.com中的所有字符串都保存在参数textstring中,并使用它。

使用HttpClient。然后,使用InputStream检查以下链接:

并将其写入所需的文件

一个快速而肮脏的例子:

// 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你能给我一个使用内部存储的例子吗?我认为使用SharedReference是我必须使用的最好的一个,但我不知道如何使用它:(我曾试图将它放入我的httpconnection类中,但它给了我错误。完成。请查看我编辑的答案。@VicentePlata我尝试了你的答案,我试图首先用toast
toast.makeText(getApplicationContext(),c,toast.LENGTH\u SHORT)。show()显示细节);
但没有响应。$然后会发生什么?数据应该保存在应用程序的路径、一个“数据”文件夹和一个名为datos.txt的文件中。该文件是否出现?@VicentePlata没有发生任何事情,也没有出现任何文件。文件应该在哪里创建?在可绘图文件夹中?或者我必须先创建文件,然后覆盖它?