Question

Below is the method which I have used to read a response from server by passing a url which retrieved a json object.

There is a very peculiar issue which is still old values has been fetched , though the data has already been updated.

I tried to find out the solution of it but still no success.

Url is of type: http://www.mywebsite.com/svc/user_auth/user_id

Where a user id is the unique integer id of the user which is being passed as a parameter.

 public static String getResponse(String url){
            String downloadedData = null;
            Log.e("getResponse", url);
            try {
                URL downloadURL = new URL(url);
                InputStream inputStream = (InputStream) downloadURL.getContent();
                if (null != inputStream) {
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    byte[] buffer = new byte[512];
                    int readCounter = inputStream.read(buffer);
                    while (readCounter != -1) {
                        byteArrayOutputStream.write(buffer, 0, readCounter);
                        readCounter = inputStream.read(buffer);
                    }
                    downloadedData = new String(
                            byteArrayOutputStream.toByteArray());
                    /*if (null != downloadedData && !"".equals(downloadedData)) {
                        downloadedJson = new JSONObject(downloadedData);
                    }*/
                }else{
                    Log.e("getResponse", "Response is null");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return downloadedData;
        }

Any help would be appreciated.

Was it helpful?

Solution

Don't know how is URL.getContent() implemented internally. You can try to use URLConnection, it allows you to control whether to use cache by setUseCaches().

OTHER TIPS

Have you tried opening the url from your browser to see the JSON response? If you see the old value in the browser then the problem is on your server side.

If this is not the case try using the DefaultHttpClient for post and get requests like: (maybe some caching occur with the method you are using, this is not the case for sure with HttpClient)

   DefaultHttpClient httpclient = getHttpClient();

    HttpPost httpost = new HttpPost(url);

    httpost.setEntity(entity);  //entity has your params wrapped if you have any...this is just an example for POST request, but for your case you can use GET with URL params..

    httpclient.execute(httpost);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top