【问题标题】:Android webservice GET request replies only a part of the XML responseAndroid webservice GET 请求仅回复部分 XML 响应
【发布时间】:2011-11-26 19:58:10
【问题描述】:

我正在尝试连接到我家的暖气提供的网络服务。将请求 URL 放入 Chrome 会生成一个完整的 XML 文件。 随后,我尝试用一​​个 Android 应用程序以编程方式执行相同的操作,不幸的是,它只回复了大约一半的 XML 文件。

我已经尝试了几次尝试,其中包括一个简单的 HttpConnection:

private void androidHttpConnect() {

    HttpURLConnection urlConnection=null;

    try {
        URL url = new URL("http://10.0.0.140:8080/user/menu");
        urlConnection = (HttpURLConnection) url.openConnection();
        BufferedInputStream in = new BufferedInputStream(
                urlConnection.getInputStream());

        Log.i("myapp",convertStreamToString(in));
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        urlConnection.disconnect();
    }
}

private String convertStreamToString(InputStream is) {
    return new Scanner(is).useDelimiter("\\A").next();
}

和 Android Http 客户端 ...

HttpClient httpclient = AndroidHttpClient.newInstance("Android");
         HttpGet httpget = new HttpGet("http://10.0.0.140:8080/user/menu");
        HttpResponse response;
        try {
            response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                long len = entity.getContentLength();
                Log.d("myapp", "content length "+len);
                if (len != -1) {
                    try {
                            Log.d("myapp", EntityUtils.toString(entity));
                        } catch (ParseException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                } else {
                    // Stream content out
                }
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

有趣的是,这些尝试在不同的位置上削减了结果,即使它们仅在大约 5 个字符上有所不同。在这个位置没有特殊字符,XML 很短。

有人知道吗?我还尝试在 ASyncTask 中运行它以确保 UI 线程不会中断,但没有成功。

感谢您的帮助。

【问题讨论】:

  • 我尝试在java桌面应用程序中运行相同的代码,它运行良好,所以问题一定与Android有关!
  • 我也面临同样的问题,只得到响应的几个元素而不是整个响应

标签: android web-services http httpclient


【解决方案1】:

终于自己找到了解决办法!问题不在于请求,而在于 LogCat 中的输出。分别记录每一行以获得所需的完整响应!

【讨论】:

  • 恭喜修复!如果有能力,请确保接受您的答案,以便其他人可以从您的成功中学习。干杯~
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-28
  • 1970-01-01
相关资源
最近更新 更多