检查Web服务是否为空

时间:2012-11-01 08:44:44

标签: android

我有一个网络服务,我想知道它是否为空? 我想这样做..如何发送请求并在textView中打印响应? 我知道必须使用HttpConnetion 但我对这堂课很困惑!

1 个答案:

答案 0 :(得分:0)

DefaultHttpClient httpClient = new DefaultHttpClient();            HttpGet httpPost = new HttpGet(urlName);

       HttpResponse httpResponse = httpClient.execute(httpPost);
       HttpEntity httpEntity = httpResponse.getEntity();
       inputStream = httpEntity.getContent(); 




       BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
       StringBuilder stringBuilder = new StringBuilder();
       String line = null;
       if ((line = reader.readLine()) != null) {
       stringBuilder.append(line + "\n");
       Data.servContent=stringBuilder.toString();
       }

       line = reader.readLine();
       inputStream.close();
      } catch (UnsupportedEncodingException e) {
           System.err.print("UnsupportedEncodingException: " + e);
       } catch (ClientProtocolException e) {
       System.err.print("ClientProtocolException: " + e);
       } catch (IOException e) {
       System.err.print("IOException: " + e);
       } catch (Exception e) {
       Log.e("Buffer Error", "Error converting result " + e.toString());
       }