当前位置: 动力学知识库 > 问答 > 编程问答 >

httpconnection - How Save Data Persistent From PHP to Android

问题描述:

How i save data from PHP to android persistently?

example:

i want all string in www.google.com saved in parameter textstring, and i use it..

网友答案:

Use HttpClient. Then, with your InputStream, check this link:

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

And write it to the file you want.

A quick and dirty example:

// 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...

You may also want to buffer your reading and writing code.

Another method would be using:

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

It's up to you. I like HttpClient 'cos you can handle more things.

分享给朋友:
您可能感兴趣的文章:
随机阅读: