2014-09-30 95 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,所以該方法在兩個類中都可用。