package com.example.btest; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { private TextView tv; String STR; final Handler h=new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub try { tv.setText(STR); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } super.handleMessage(msg); } }; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv=(TextView)findViewById(R.id.bbb); new Thread(){ public void run(){ try {STR=captureHtml(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } h.sendMessage(h.obtainMessage()); } }.start(); } public String captureHtml() throws Exception { String strURL = "http://172.16.2.38:18655/hq/hqV.jsp"; URL url = new URL(strURL); HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); InputStreamReader input = new InputStreamReader(httpConn.getInputStream(), "GB2312"); BufferedReader bufReader = new BufferedReader(input); String line = ""; StringBuilder contentBuf = new StringBuilder(); while ((line = bufReader.readLine()) != null) { contentBuf.append(line); } String buf = contentBuf.toString(); System.out.println("captureHtml()的结果: " +buf); return buf; } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }