ANDROID_MARS学习笔记_S04_003_用HttpClent发http请求

一、代码

1.xml
(1)activity_main.xml

 1     <TextView
 2         android:layout_width="wrap_content"
 3         android:layout_height="wrap_content"
 4         android:text="发送Http请求"
 5         android:onClick="sendHttpRequest" />
 6     <Button
 7         android:layout_width="wrap_content"
 8         android:layout_height="wrap_content"
 9         android:text="发送Http请求"
10         android:onClick="sendHttpRequest" />
11 
12 </LinearLayout>

 

(2)AndroidManifest.xml

增加

1 <uses-permission android:name="android.permission.INTERNET"/>

 

2.java
(1)MainActivity.java

 1 package com.http;
 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.annotation.SuppressLint;
16 import android.app.Activity;
17 import android.os.Bundle;
18 import android.os.StrictMode;
19 import android.view.View;
20 
21 @SuppressLint("NewApi")
22 public class MainActivity extends Activity {
23 
24     @Override
25     protected void onCreate(Bundle savedInstanceState) {
26         super.onCreate(savedInstanceState);
27         setContentView(R.layout.activity_main);
28         if (android.os.Build.VERSION.SDK_INT > 9) {
29             StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
30             StrictMode.setThreadPolicy(policy);
31         }
32     }
33     
34     public void sendHttpRequest(View view) {
35         System.out.println("sendHttpRequest---->");
36         //生成一个请求对象
37         HttpGet httpGet = new HttpGet("http://www.baidu.com");
38         //生成一个Http客户端对象
39         HttpClient httpClient = new DefaultHttpClient();
40         InputStream inputStream = null;
41         //使用Http客户端发送请求对象
42         try {
43             HttpResponse httpResponse = httpClient.execute(httpGet);
44             HttpEntity httpEntity = httpResponse.getEntity();
45             inputStream = httpEntity.getContent();
46             BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
47             String line = null;
48             StringBuilder sb = new StringBuilder();
49             while ((line = reader.readLine()) != null) {
50                 sb.append(line);
51             }
52             System.out.println(sb);
53         } catch (ClientProtocolException e) {
54             e.printStackTrace();
55         } catch (IOException e) {
56             e.printStackTrace();
57         } finally {
58             try {
59                 inputStream.close();
60             } catch (IOException e) {
61                 e.printStackTrace();
62             }
63         }
64         
65     }
66 }

 

 

 

posted @ 2016-02-21 15:26  shamgod  阅读(363)  评论(0编辑  收藏  举报
haha