How to clear cache only for specific services in android programatically

633 views Asked by At

My app fetches data from a server, usually by calling a service, or URL using the POST method, in which the parameters are added to the URL. For this I have used a JSONParser class that make a request for the data to the server, and then gets the data in a JSON format, like the following code:

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    JSONArray jArr = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    public JSONArray getJSONFromUrl(String url) {

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);


            HttpResponse httpResponse = httpClient.execute(httpPost);

            HttpEntity httpEntity = httpResponse.getEntity();

            is = httpEntity.getContent();           

        } catch (UnsupportedEncodingException e) {

        } catch (ClientProtocolException e) {

        } catch (IOException e) {

        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is,  "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
            Log.e("JSON Parser", json );
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            JSONTokener jt = new JSONTokener(json);
            Object rootElement = jt.nextValue();
            if (rootElement instanceof JSONObject) {
               // You got an object from the jresponse
                jObj = new JSONObject(json);
            } else if (rootElement instanceof JSONArray) {
                 jArr = new JSONArray(json);

                 return jArr;
               // You got a JSON array
            }
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jArr;

    }
}

To call this class , I use the following piece of code:

String njoftime_url = URL[0];

                // Creating JSON Parser instance
                JSONParser jParser = new JSONParser();

                // getting JSON string from URL
                njoftimeInformation = jParser.getJSONFromUrl(njoftime_url);
...................

The problem is that, when I call the service for the first time, I receive some data, well-formatted, but when disconnect the phone from the internet, and I make a call, I still receive the same data, even if the phone is disconnected.

My guess is that the data are saved in cache of the program in Android, but sometimes this gives me the wrong data, for example when I change the parameter from:

http://www.server.com/path/service_name.ashx?code=MAKINA_CATEGORY

to:

http://www.server.com/path/service_name.ashx?code=Prona_CATEGORY

and there is no connection to internet, or connection is lost it gives me the data according to the first parameter, which for my guess are saved in the cache.

My question is how to delete such data save in cache only for some services and URL.

Any Help would be appreciated.

0

There are 0 answers