2011-11-26 50 views
2

我试图连接到由我在家供暖的web服务。将请求URL放入Chrome会生成一个完整的XML文件。 随后,我试图用Android应用程序编程,但不幸只回复了一半的XML文件。Android webservice GET请求仅回复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线程没有中断,但没有成功。

感谢您的帮助。

+0

我试图在java桌面应用程序中运行相同的代码,它工作正常,所以问题必须与Android相关联! – user1033552

+0

我也面临同样的问题,得到的回应不是整个回应的几个元素 – Taruni

回答

0

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

+0

祝贺修复!如果可以,请确保接受您的答案,以便其他人可以从您的成功中学习。干杯〜 –