JSON被android应用程序缓存 [英] JSON being cached by android application

查看:113
本文介绍了JSON被android应用程序缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前的Android应用程序存在问题,如果我运行应用程序,服务器在线,然后用后退按钮关闭应用程序,关闭服务器,然后再次启动应用程序,json回应被记住了。如果我强制退出应用程序,并且服务器关闭再次运行它,那么没有json响应。这个响应被缓存在哪里?我通过实例方法调用它来接收此响应,我不明白为什么会发生这种情况。我怎么能让我的应用程序不记得这个回复?

I'm currently having an issue with my android application where if I run the app, with the server online, and then close the app with the back button, and turn the server off and then start the app again, the json response is remembered. If I force quit the app, and with the server off run it again, then there is no json response. Where is this response being cached? I am calling it through an instance method to receive this response and I do not understand why this is happening. How can I have my app not remember this response?

在我的活动中:

    uFunctions = new JSONFunctions();
    JSONObject json = null;
    System.out.println(json); // Returns null
    json = uFunctions.getJson(uid);
    System.out.println(json); //Returns the json after the request was completed
    //successfully even if the server is offline afterwards.

JSONFunctions:

JSONFunctions:

   public class JSONFunctions{

      public JSONFunctions(){
            jParser = new jParser();
      }

     public JSONObject getJson(String uid){
     List<NameValuePair> param = new ArrayList<NameValuePair>();
     param.add(new BasicNameValuePair("tag", "request");
     param.add(new BasicNameValuePair("id", uid);
     JSONObject json = jParser.getJsonFromUrl(url, param);
     return json;
     }
     }

jParser:

public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {

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

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 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", json);
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);            
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}


推荐答案

HTTP响应被缓存在系统中;正如Jayp指出的那样,后退按钮不一定会从内存中刷新你的应用程序,因此缓存的响应仍然存在。

HTTP responses are cached in the system; as Jayp has pointed out, the back button doesn't necessarily flush your app from memory, so the cached response is still there.

如果你正在使用Apache库,在您的http请求对象中禁用缓存:

If you're using the Apache library, disable caching in your http request object:

  request.setHeader("Cache-Control", "no-cache");

这应该可以解决问题。我认为对于java.net,你会在URLConnection中禁用缓存:

That should do the trick. I think for java.net, you would disable caching in URLConnection:

  conn.setUseCaches(false);

但我还没有真正尝试过。

but I haven't actually tried it yet.

这篇关于JSON被android应用程序缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆