1 package com.android.tools;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6
7 import org.apache.http.HttpEntity;
8 import org.apache.http.HttpResponse;
9 import org.apache.http.client.ClientProtocolException;
10 import org.apache.http.client.HttpClient;
11 import org.apache.http.client.methods.HttpGet;
12 import org.apache.http.client.methods.HttpPost;
13 import org.apache.http.impl.client.DefaultHttpClient;
14 import org.apache.http.params.HttpConnectionParams;
15 import org.apache.http.params.HttpParams;
16
17 /**
18 * 网络访问工具
19 *
20 * @author Administrator
21 *
22 */
23 public class HttpUtil {
24
25 /**
26 * 创建get请求对象
27 *
28 * @param url
29 * @return
30 */
31 public static HttpGet getHttpGet(String url) {
32 HttpGet request = new HttpGet(url);
33 return request;
34 }
35
36 /**
37 * 创建post请求对象
38 *
39 * @param url
40 * @return
41 */
42 public static HttpPost getHttpPost(String url) {
43 HttpPost request = new HttpPost(url);
44 return request;
45 }
46
47 /**
48 * 根据请求获得响应对象
49 *
50 * @return
51 * @throws IOException
52 * @throws ClientProtocolException
53 */
54 public static HttpResponse getHttpResponse(HttpGet request)
55 throws ClientProtocolException, IOException {
56 HttpResponse response = new DefaultHttpClient().execute(request);
57 return response;
58 }
59
60 /**
61 * 根据请求获得响应对象
62 *
63 * @param request
64 * @return
65 * @throws ClientProtocolException
66 * @throws IOException
67 */
68 public static HttpResponse getHttpResponse(HttpPost request)
69 throws ClientProtocolException, IOException {
70 HttpResponse response = new DefaultHttpClient().execute(request);
71 return response;
72 }
73
74 /**
75 * 发送Post请求,获取响应结果
76 *
77 * @param url
78 * @return
79 * @throws IOException
80 * @throws ClientProtocolException
81 */
82 public static String queryStringForPost(String url)
83 throws ClientProtocolException, IOException {
84 // 根据URL获取POST对象
85 HttpPost request = getHttpPost(url);
86 String result = null;
87
88 try {
89 // 获得响应对象
90 HttpResponse response = getHttpResponse(request);
91 // 判断是否请求成功
92 if (response.getStatusLine().getStatusCode() == 200) {
93 // 获得响应
94 result = "获得数据";
95 return result;
96 }
97 } catch (ClientProtocolException e) {
98 e.printStackTrace();
99 result = "网络异常!";
100 return result;
101 } catch (IOException e) {
102 e.printStackTrace();
103 result = "网络异常!";
104 return result;
105 }
106
107 return null;
108 }
109
110 /**
111 * 发送GET请求,获取响应结果
112 *
113 * @param url
114 * @return
115 * @throws IOException
116 * @throws ClientProtocolException
117 */
118 public static String queryStringForGet(String url) {
119 // 根据URL获取POST对象
120 HttpGet request = getHttpGet(url);
121 String result = null;
122
123 try {
124 // 获得响应对象
125 HttpResponse response = getHttpResponse(request);
126 // 判断是否请求成功
127 if (response.getStatusLine().getStatusCode() == 200) {
128 // 获得响应
129 result = "success";
130 return result;
131 }
132 } catch (ClientProtocolException e) {
133 e.printStackTrace();
134 result = "网络异常!";
135 return result;
136 } catch (IOException e) {
137 e.printStackTrace();
138 result = "网络异常!";
139 return result;
140 }
141
142 return null;
143 }
144
145
146 public static String getContent(String url) throws IOException{
147 StringBuilder sb = new StringBuilder();
148 HttpClient client = new DefaultHttpClient();
149 HttpParams httpParams = client.getParams();
150
151 //设置网络连接参数及超时时间为5秒
152 HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
153
154 try {
155 HttpResponse response = client.execute(new HttpGet(url));
156 HttpEntity entity = response.getEntity();
157 if(entity != null){
158 BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(),"utf8"),8192);
159 String line = null;
160 while((line = reader.readLine())!=null){
161 sb.append(line + "\n");
162 }
163 reader.close();
164 }
165 }
166 catch(ClientProtocolException e){
167 e.printStackTrace();
168 }
169 catch(IOException e){
170 e.printStackTrace();
171 }
172 return sb.toString();
173 }
174 }