2014-09-30 65 views
-1

这是一个非常大的问题,它真的困扰着我。 我有一个web服务,我通过josn从我的数据中获取数据,我需要我的数据进行更新。android-如何避免缓存json

问题是这样的,我打开我的应用程序,它连接到webservice,它会得到非常旧的数据,我删除应用程序并再次安装它,清除缓存和数据,没有任何工作,它仍然会得到旧数据。

我该如何解决? 这是获取数据的代码:

public class Spots_tab1_json { 
static String response = null; 
public final static int GET = 1; 
public final static int POST = 2; 

public Spots_tab1_json() { 

} 
public String makeServiceCall(String url, int method) { 
    return this.makeServiceCall(url, method, null); 
} 
public String makeServiceCall(String url, int method, 
     List<NameValuePair> params) { 
    try { 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpEntity httpEntity = null; 
     HttpResponse httpResponse = null; 
     if (method == POST) { 
      HttpPost httpPost = new HttpPost(url); 
      if (params != null) { 
       httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8")); 
      } 
      httpResponse = httpClient.execute(httpPost); 
     } else if (method == GET) { 
      if (params != null) { 
       String paramString = URLEncodedUtils.format(params, "UTF-8"); 
       url += "?" + paramString; 
      } 
      HttpGet httpGet = new HttpGet(url); 
      httpResponse = httpClient.execute(httpGet); 

     } 
     httpEntity = httpResponse.getEntity(); 
     response = EntityUtils.toString(httpEntity); 

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

    return response; 

} 

}

感谢

+0

请命名约定,这会伤害我的眼睛:class Spots_tab1_json ... – 2Dee 2014-09-30 08:10:40

回答

-1

当你创建新的HTTP你有下面的代码注销第一次使用disconnect

client.getConnectionManager().closeExpiredConnections(); 
client = null; 

和然后创建新的HTTP connection

+0

或者我希望你可以使用conn.setUseCaches(false); – Boldbayar 2014-09-30 07:58:57

0

尝试将Cache-Control标题添加到您的请求中,并且值为no-cache,例如, httpGet.addHeader("Cache-Control", "no-cache")HttpGetHttpPost都继承了AbstractHttpMessage,所以该方法在两个类中都可用。