0

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

4

1 回答 1

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());
       }
于 2012-11-06T13:16:56.343 回答