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.
精彩评论