Android进阶2之Http操作访问网络

Android进阶2之Http操作访问网络_第1张图片

操作步骤:

<1>

生成请求对象

HttpGet httpGet = new HttpGet("请求地址。。。。。");

<2>

生成客户端对象

HttpClient httpClient = new DefaultHttpClient();

<3>

执行请求

HttpResponse httpResponse = httpClient.execute(httpGet);

<4>

接受响应

HttpEntity  httpEntity = httpResponse.getEntity();

<5>得到数据流

InputStream  inputStream = httpEntity.getContent();

注意:

要添加权限: <uses-permission android:name="android.permission.INTERNET" />

具体实现:

[java] view plain copy print ?
  1. package xiaosi.httpResponse;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.io.InputStreamReader;  
  7.   
  8. import org.apache.http.HttpEntity;  
  9. import org.apache.http.HttpResponse;  
  10. import org.apache.http.client.ClientProtocolException;  
  11. import org.apache.http.client.HttpClient;  
  12. import org.apache.http.client.methods.HttpGet;  
  13. import org.apache.http.impl.client.DefaultHttpClient;  
  14.   
  15. import android.app.Activity;  
  16. import android.os.Bundle;  
  17. import android.view.View;  
  18. import android.view.View.OnClickListener;  
  19. import android.widget.Button;  
  20. import android.widget.TextView;  
  21.   
  22. public class HttpResponseActivity extends Activity  
  23. {  
  24.     private Button          button          = null;  
  25.     private TextView        text            = null;  
  26.     private HttpResponse    httpResponse    = null;  
  27.     private HttpEntity      httpEntity      = null;  
  28.   
  29.     @Override  
  30.     public void onCreate(Bundle savedInstanceState)  
  31.     {  
  32.         super.onCreate(savedInstanceState);  
  33.         setContentView(R.layout.main);  
  34.         text = (TextView) findViewById(R.id.text);  
  35.         button = (Button) findViewById(R.id.button);  
  36.         button.setOnClickListener(new OnClickListener() {  
  37.             @Override  
  38.             public void onClick(View v)  
  39.             {  
  40.                 // 生成一个请求对象,参数就是地址   
  41.                 HttpGet httpGet = new HttpGet("http://www.baidu.com");  
  42.                 // 生成Http客户端   
  43.                 HttpClient httpClient = new DefaultHttpClient();  
  44.                 InputStream inputStream = null;  
  45.                 // 使用HTTP客户端发送请求对象  
  46.                 try  
  47.                 {  
  48.                     // 发送请求的响应   
  49.                     httpResponse = httpClient.execute(httpGet);  
  50.                     // 代表接收的http消息,服务器返回的消息都在httpEntity  
  51.                     httpEntity = httpResponse.getEntity();  
  52.                     if(httpResponse.getStatusLine().getStatusCode() == 200){  
  53.                         inputStream = httpEntity.getContent();  
  54.                         BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));  
  55.                         String result = "";  
  56.                         String line = "";  
  57.                         while ((line = reader.readLine()) != null)  
  58.                         {  
  59.                             result = result + line;  
  60.                         }  
  61.                           
  62.                         text.setText(result);  
  63.                     }  
  64.                 }  
  65.                 catch (ClientProtocolException e)  
  66.                 {  
  67.                     e.printStackTrace();  
  68.                 }  
  69.                 catch (Exception e)  
  70.                 {  
  71.                     e.printStackTrace();  
  72.                 }  
  73.                 finally  
  74.                 {  
  75.                     try  
  76.                     {  
  77.                         inputStream.close();  
  78.                     }  
  79.                     catch (IOException e)  
  80.                     {  
  81.                         e.printStackTrace();  
  82.                     }  
  83.                 }  
  84.             }  
  85.         });  
  86.     }  
  87. }  

你可能感兴趣的:(Android进阶2之Http操作访问网络)