0

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

Livi
  • 17
  • 2
  • 6

1 Answers1

0

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.

Vicente Plata
  • 3,370
  • 1
  • 19
  • 26
  • @VicentePlata Can u give me a example using internal storage? i think the best one i must using sharedpreference, but i don't know how to used it :( i had try to put it in my httpconnection class, but it give me error. – Livi Apr 23 '11 at 03:58
  • @VicentePlata i had try your answer, i try to show the detail first with toast Toast.makeText(getApplicationContext(), c,Toast.LENGTH_SHORT).show(); but there's no response. :$ – Livi Apr 23 '11 at 06:48
  • What happens then? The data should be saved in your app's path, a "data" folder and a file called datos.txt . Does that file appears? – Vicente Plata Apr 23 '11 at 15:18
  • @VicentePlata nothing happened and no file appears.. where should be the file created? in drawable folder? or may be i must create the file first and then overwrite it? – Livi Apr 25 '11 at 02:05
  • The file must be created on your device's File System. To get the exact path, try by calling getApplicationContext().getFilesDir().getPath() in a Toast or something. Then by using the DDMS perspective in Eclipse, look for it. – Vicente Plata Apr 25 '11 at 02:08
  • @VicentePlata i got it, there is a file datos.txt in DDMS, but i can't use it with InputStream is = this.getResources().openRawResource(datos); , bcoz R.java cannot recognized it. can it just saved at drawable or something so R.java can recognize the files? – Livi Apr 25 '11 at 03:10
  • You're not supposed to do so. Those resources are not accesible for writing: http://stackoverflow.com/questions/3760626/how-to-write-files-to-assets-folder-or-raw-folder-in-android . I don't know why you need them though, if you can read the file and get it as a String. – Vicente Plata Apr 25 '11 at 03:23
  • @VicentePlata so the file cannot be used? because what i need is get String from a web file, that i can use to listview. – Livi Apr 25 '11 at 03:30
  • Of course you can use it. The file is there and you can open it, read it, and display the text according to your need. – Vicente Plata Apr 25 '11 at 13:43