Pranay Airan
Pranay Airan

Reputation: 1875

download json file and store locally on my android device

I am doing a rest call and getting a json file from the server by this

HttpGet httpget = new HttpGet("someurl");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
InputStream source = entity.getContent();

now i want to store this into a local file I am using FileOutputStream to do this but the problem is how to effectively convert the inputstream to outputstream if i am using something like this

FileOutputStream fos = openFileOutput(filename,Context.MODE_PRIVATE);

            int nextChar;
            while ((nextChar = source.read()) != -1) {
                fos.write((char) nextChar);
                System.out.println((char) nextChar);
                fos.flush();
            }

it is storing very slow the file which i am getting is upto 100kb is there any other faster method or any other way which i can use to store the json file in my device?

My applications uses this json heavily and i don't want to call the REST each time.

thanks Pranay

Upvotes: 1

Views: 3894

Answers (1)

user370305
user370305

Reputation: 109237

try to use org.apache.commons.io.IOUtils's

    IOUtils.copy(is,fos); 

lets see, what happening. Thanks,

EDIT: Why not you are use sqlite database? parse JSON result onetime and insert it in database only onetime stuff then always you get fast execution.

EDIT: try android's internal storage for write json file

  try {      
       FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_APPEND);
        fos.write(myJSONString.getBytes());
         fos.close();      
       //Log.d(TAG, "Written to file");  
     } 
   catch (Exception e)
     {    
        Log.d(TAG, "cought");     
         e.printStackTrace(); 
      } 

Upvotes: 1

Related Questions