packagecom.example.http;importjava.io.BufferedReader;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.net.HttpURLConnection;importjava.net.URL;importorg.apache.http.HttpEntity;importorg.apache.http.HttpResponse;importorg.apache.http.client.HttpClient;importorg.apache.http.client.methods.HttpGet;importorg.apache.http.impl.client.DefaultHttpClient;importorg.apache.http.util.EntityUtils;importandroid.os.Bundle;importandroid.os.Handler;importandroid.os.Message;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.widget.Button;importandroid.widget.TextView;importandroid.annotation.SuppressLint;importandroid.app.Activity;@SuppressLint("HandlerLeak")publicclassMainActivityextendsActivity{privatestaticfinalintSHOW_RESPONSE=0;privateTextViewtextView;privateButtonbutton;privateHandlerhandler=newHandler(){publicvoidhandleMessage(Messagemsg){switch(msg.what){caseSHOW_RESPONSE:Stringresponse=(String)msg.obj;textView.setText(response);break;}}};@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textView=(TextView)findViewById(R.id.webview);button=(Button)findViewById(R.id.button);button.setOnClickListener(newOnClickListener(){@OverridepublicvoidonClick(Viewv){//sendRequestWithHttpConnection();sendRequestWithHttpClient();}});}/***httpclient网络访问*/protectedvoidsendRequestWithHttpClient(){newThread(newRunnable(){@Overridepublicvoidrun(){try{HttpClienthttpClient=newDefaultHttpClient();HttpGethttpGet=newHttpGet("http://www.baidu.com");HttpResponsehttpResponse=httpClient.execute(httpGet);if(httpResponse.getStatusLine().getStatusCode()==200){//请求成功HttpEntityentity=httpResponse.getEntity();Stringresponse=EntityUtils.toString(entity,"utf-8");Messagemessage=newMessage();message.what=SHOW_RESPONSE;message.obj=response.toString();handler.sendMessage(message);}}catch(Exceptione){e.printStackTrace();}finally{}}}).start();}/***httpURLConnection网络访问*/protectedvoidsendRequestWithHttpConnection(){newThread(newRunnable(){@Overridepublicvoidrun(){HttpURLConnectionconnection=null;try{URLurl=newURL("http://www.baidu.com");connection=(HttpURLConnection)url.openConnection();connection.setRequestMethod("GET");connection.setConnectTimeout(8000);connection.setReadTimeout(8000);connection.setDoInput(true);connection.setDoOutput(true);InputStreaminputStream=connection.getInputStream();BufferedReaderreader=newBufferedReader(newInputStreamReader(inputStream));StringBufferresponse=newStringBuffer();Stringline;while((line=reader.readLine())!=null){response.append(line);}Messagemessage=newMessage();message.what=SHOW_RESPONSE;message.obj=response.toString();handler.sendMessage(message);}catch(Exceptione){e.printStackTrace();}finally{if(connection!=null){connection.disconnect();}}}}).start();}}布局:<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:id="@+id/button"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="sendrequest"/><ScrollViewandroid:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid:id="@+id/webview"android:layout_width="match_parent"android:layout_height="wrap_content"/></ScrollView></LinearLayout>