Android webservice GET request replies only a part of the XML response

廉价感情. 提交于 2020-02-04 01:30:10

问题


I'm trying to connect to a webservice offered by my heating at home. Putting the request URL into Chrome results in a complete XML file. Subsequently I tried to do the same programmatically with an Android application, which unfortunately only replies about the half of the XML file.

I already tried several attempts, amongst others a simple 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();
}

and the Android Http Client ...

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

Interestingly those attempts cut the result on different positions, even though they only differ in about 5 characters. At this position there is no special character and the XML is quite short.

Anyone any idea? I also tried to run it in a ASyncTask to ensure no interrupt by the UI thread, but without success.

Thanks for your help.


回答1:


Finally found the solution by myself! The problem wasn't the request but the output in the LogCat. Logging every line separately obtained the desired full response!



来源:https://stackoverflow.com/questions/8281186/android-webservice-get-request-replies-only-a-part-of-the-xml-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!