-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebAccessTools.java
More file actions
68 lines (62 loc) · 2.27 KB
/
WebAccessTools.java
File metadata and controls
68 lines (62 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.example.tester;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
/**
* Created by toeknee on 13-8-2.
*/
public class WebAccessTools {
/**
* 当前的Context上下文对象
*/
private Context context;
/**
* 构造一个网站访问工具类
*/
public WebAccessTools() {
}
/**
* 根据给定的url地址访问网络,得到响应内容(这里为GET方式访问)
* @param url 指定的url地址
* @return web服务器响应的内容,为<code>String</code>类型,当访问失败时,返回为null
*/
public String getWebContent(String url) {
//创建一个http请求对象
HttpGet request = new HttpGet(url);
//创建HttpParams以用来设置HTTP参数
HttpParams params = new BasicHttpParams();
//创建一个网络访问处理对象
HttpClient httpClient = new DefaultHttpClient(params);
try{
//执行请求参数项
HttpResponse response = httpClient.execute(request);
Log.v("ResponseCode", response.getStatusLine()
.getStatusCode() + "");
//判断是否请求成功
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//获得响应信息
String content = EntityUtils.toString(response.getEntity());
Log.d("CareHelper", content);
return content;
} else {
//网连接失败,使用Toast显示提示信息
Toast.makeText(context, R.string.ConnectError, Toast.LENGTH_LONG).show();
Log.d("CareHelper", "Failure in connecting to Internet.");
}
} catch(Exception e) {
e.printStackTrace();
} finally {
//释放网络连接资源
httpClient.getConnectionManager().shutdown();
}
return null;
}
}