2014-01-06 31 views
0

下面是我用來通過傳遞一個檢索到json對象的url來讀取來自服務器的響應的方法。Android:Http獲取舊響應

雖然數據已經更新,但仍有一個非常奇怪的問題,即舊值已被提取。

我試圖找出它的解決方案,但仍然沒有成功。

鏈接類型:http://www.mywebsite.com/svc/user_auth/user_id

,其中用戶ID是被作爲參數傳遞的用戶的唯一整數標識符。

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; 
     } 

任何幫助,將不勝感激。

回答

0

不知道如何在內部實現URL.getContent()。您可以嘗試使用URLConnection,它允許您通過setUseCaches()控制是否使用緩存。

0

您是否曾嘗試從瀏覽器中打開url以查看JSON響應?如果您在瀏覽器中看到舊值,那麼問題出在您的服務器端。

如果不是這種情況下,嘗試使用DefaultHttpClient的職位,並得到類似請求:(也許有些緩存出現與您所使用的方法,這是不肯定的情況下與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);