2

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.

user1033552
  • 1,499
  • 1
  • 14
  • 23
  • I tried to run the same code in a java desktop application and it works fine, so the problem must be associated with Android! – user1033552 Nov 27 '11 at 10:24
  • i am also facing the same problem,getting only few elements of the response not the entire response – Taruni Nov 28 '11 at 12:38

1 Answers1

0

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!

user1033552
  • 1,499
  • 1
  • 14
  • 23
  • Congrats on the fix! When you are able, make sure to accept your answer so that others can learn from your success. Cheers~ – Andrew Kozak Nov 28 '11 at 19:58