http请求方式httpURLContention和httpClient

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lg491733638/article/details/47665017
package  com.example.http;
import  java.io.BufferedReader;
import  java.io.InputStream;
import  java.io.InputStreamReader;
import  java.net.HttpURLConnection;
import  java.net.URL;
import  org.apache.http.HttpEntity;
import  org.apache.http.HttpResponse;
import  org.apache.http.client.HttpClient;
import  org.apache.http.client.methods.HttpGet;
import  org.apache.http.impl.client.DefaultHttpClient;
import  org.apache.http.util.EntityUtils;
import  android.os.Bundle;
import  android.os.Handler;
import  android.os.Message;
import  android.view.View;
import  android.view.View.OnClickListener;
import  android.widget.Button;
import  android.widget.TextView;
import  android.annotation.SuppressLint;
import  android.app.Activity;
@SuppressLint ( "HandlerLeak" )
public  class  MainActivity  extends  Activity {
  private  static  final  int  SHOW_RESPONSE =  0 ;
  private  TextView textView;
  private  Button button;
  private  Handler handler =  new  Handler() {
   public  void  handleMessage(Message msg) {
    switch  (msg.what) {
    case  SHOW_RESPONSE:
     String response = (String) msg.obj;
     textView.setText(response);
     break ;
    }
   }
  };
  @Override
  protected  void  onCreate(Bundle savedInstanceState) {
   super .onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   textView = (TextView) findViewById(R.id.webview);
   button = (Button) findViewById(R.id.button);
   button.setOnClickListener( new  OnClickListener() {
    @Override
    public  void  onClick(View v) {
     // sendRequestWithHttpConnection();
     sendRequestWithHttpClient();
    }
   });
  }
  /**
   * httpclient网络访问
   */
  protected  void  sendRequestWithHttpClient() {
   new  Thread( new  Runnable() {
    @Override
    public  void  run() {
     try  {
      HttpClient httpClient =  new  DefaultHttpClient();
      HttpGet httpGet =  new  HttpGet( "http://www.baidu.com" );
      HttpResponse httpResponse = httpClient.execute(httpGet);
      if  (httpResponse.getStatusLine().getStatusCode() ==  200 ) {
       // 请求成功
       HttpEntity entity = httpResponse.getEntity();
       String response = EntityUtils.toString(entity,  "utf-8" );
       Message message =  new  Message();
       message.what = SHOW_RESPONSE;
       message.obj = response.toString();
       handler.sendMessage(message);
      }
     catch  (Exception e) {
      e.printStackTrace();
     finally  {
     }
    }
   }).start();
  }
  /**
   * httpURLConnection网络访问
   */
  protected  void  sendRequestWithHttpConnection() {
   new  Thread( new  Runnable() {
    @Override
    public  void  run() {
     HttpURLConnection connection =  null ;
     try  {
      URL url =  new  URL( "http://www.baidu.com" );
      connection = (HttpURLConnection) url.openConnection();
      connection.setRequestMethod( "GET" );
      connection.setConnectTimeout( 8000 );
      connection.setReadTimeout( 8000 );
      connection.setDoInput( true );
      connection.setDoOutput( true );
      InputStream inputStream = connection.getInputStream();
      BufferedReader reader =  new  BufferedReader(
        new  InputStreamReader(inputStream));
      StringBuffer response =  new  StringBuffer();
      String line;
      while  ((line = reader.readLine()) !=  null ) {
       response.append(line);
      }
      Message message =  new  Message();
      message.what = SHOW_RESPONSE;
      message.obj = response.toString();
      handler.sendMessage(message);
     catch  (Exception e) {
      e.printStackTrace();
     finally  {
      if  (connection !=  null ) {
       connection.disconnect();
      }
     }
    }
   }).start();
  }
}
布局:
<LinearLayout xmlns: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"
     >
      
     <Button 
         android:id= "@+id/button"
         android:layout_width= "match_parent"
         android:layout_height= "wrap_content"
         android:text= "send request"
         />
      
      
     <ScrollView
         android:layout_width= "match_parent"
         android:layout_height= "wrap_content"  >
         <TextView
             android:id= "@+id/webview"
             android:layout_width= "match_parent"
             android:layout_height= "wrap_content"  />
     </ScrollView>
</LinearLayout>

猜你喜欢

转载自blog.csdn.net/lg491733638/article/details/47665017