android – how to update or download the json file from url request to a local directory?-ThrowExceptions

Exception or error:

I am trying to make a mobile application, the data i am using is a huge nested JSON object which is a live API on some website. Instead of calling URL request to get the data, i have downloaded the json file and stored it on my asset folder in android. As the data was so huge thats why I stored it locally in the asset folder to avoid a lag in a request. Problem is, i have stored it locally and if the data get updated in website it wont come up to me. i want to make some kind of a method where an app user can update the data itself.

public void updatingJSONFile(){
    HttpURLConnection urlConnection;
    InputStream in = null;
    try{

        //the url we wish to connect to
        URL url = new URL("my URL");
        //open the connnection to the specific url
        urlConnection = (HttpURLConnection) url.openConnection();
        //get the response from the server in the input stream
        in = new BufferedInputStream(urlConnection.getInputStream());

    }catch(IOException e){`enter code here`
        e.printStackTrace();
    }
    // convert the input stream to a string
    String response = convertStreamToString(in);
    // print the response to the android monitor/logcat
    System.out.print("Server response = " + response);

    try {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("config.txt", Context.MODE_PRIVATE));
        outputStreamWriter.write(response);
        outputStreamWriter.close();
    }
    catch (IOException e) {
        Log.e("Exception", "File write failed: " + e.toString());
    }
}
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *