【问题标题】:Android:Http get fetches old responseAndroid:Http获取旧响应
【发布时间】:2014-01-06 14:20:10
【问题描述】:

以下是我用来通过传递检索到 json 对象的 url 从服务器读取响应的方法。

有一个非常特殊的问题,即旧值仍然被获取,尽管数据已经更新。

我试图找出它的解决方案,但仍然没有成功。

网址类型:http://www.mywebsite.com/svc/user_auth/user_id

其中用户 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;
        }

任何帮助将不胜感激。

【问题讨论】:

    标签: android web-services http-get


    【解决方案1】:

    不知道 URL.getContent() 是如何在内部实现的。你可以尝试使用URLConnection,它可以让你通过setUseCaches()来控制是否使用缓存。

    【讨论】:

      【解决方案2】:

      您是否尝试过从浏览器打开 URL 以查看 JSON 响应?如果您在浏览器中看到旧值,则问题出在您的服务器端。

      如果不是这种情况,请尝试使用 DefaultHttpClient 进行 post 和 get 请求,例如:(您使用的方法可能会发生一些缓存,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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-10-01
        • 2022-06-28
        • 2020-08-02
        • 1970-01-01
        • 2012-11-07
        • 1970-01-01
        • 2020-08-10
        相关资源
        最近更新 更多