/** |
002 |
*
@author Dylan |
003 |
*
本类封装了Android中向web服务器提交数据的两种方式四种方法 |
004 |
*/ |
005 |
public class SubmitDataByHttpClientAndOrdinaryWay
{ |
006 |
007 |
/** |
008 |
*
使用get请求以普通方式提交数据 |
009 |
*
@param map 传递进来的数据,以map的形式进行了封装 |
010 |
*
@param path 要求服务器servlet的地址 |
011 |
*
@return 返回的boolean类型的参数 |
012 |
*
@throws Exception |
013 |
*/ |
014 |
public Boolean
submitDataByDoGet(Map<String, String> map, String path) throws Exception
{ |
015 |
//
拼凑出请求地址 |
016 |
StringBuilder
sb = new StringBuilder(path); |
017 |
sb.append( "?" ); |
018 |
for (Map.Entry<String,
String> entry : map.entrySet()) { |
019 |
sb.append(entry.getKey()).append( "=" ).append(entry.getValue()); |
020 |
sb.append( "&" ); |
021 |
} |
022 |
sb.deleteCharAt(sb.length()
- 1 ); |
023 |
String
str = sb.toString(); |
024 |
System.out.println(str); |
025 |
URL
Url = new URL(str); |
026 |
HttpURLConnection
HttpConn = (HttpURLConnection) Url.openConnection(); |
027 |
HttpConn.setRequestMethod( "GET" ); |
028 |
HttpConn.setReadTimeout( 5000 ); |
029 |
//
GET方式的请求不用设置什么DoOutPut()之类的吗? |
030 |
if (HttpConn.getResponseCode()
== HttpURLConnection.HTTP_OK) { |
031 |
return true ; |
032 |
} |
033 |
return false ; |
034 |
} |
035 |
036 |
/** |
037 |
*
普通方式的DoPost请求提交数据 |
038 |
*
@param map 传递进来的数据,以map的形式进行了封装 |
039 |
*
@param path 要求服务器servlet的地址 |
040 |
*
@return 返回的boolean类型的参数 |
041 |
*
@throws Exception |
042 |
*/ |
043 |
public Boolean
submitDataByDoPost(Map<String, String> map, String path) throws Exception
{ |
044 |
//
注意Post地址中是不带参数的,所以newURL的时候要注意不能加上后面的参数 |
045 |
URL
Url = new URL(path); |
046 |
//
Post方式提交的时候参数和URL是分开提交的,参数形式是这样子的:name=y&age=6 |
047 |
StringBuilder
sb = new StringBuilder(); |
048 |
//
sb.append("?"); |
049 |
for (Map.Entry<String,
String> entry : map.entrySet()) { |
050 |
sb.append(entry.getKey()).append( "=" ).append(entry.getValue()); |
051 |
sb.append( "&" ); |
052 |
} |
053 |
sb.deleteCharAt(sb.length()
- 1 ); |
054 |
String
str = sb.toString(); |
055 |
056 |
HttpURLConnection
HttpConn = (HttpURLConnection) Url.openConnection(); |
057 |
HttpConn.setRequestMethod( "POST" ); |
058 |
HttpConn.setReadTimeout( 5000 ); |
059 |
HttpConn.setDoOutput( true ); |
060 |
HttpConn.setRequestProperty( "Content-Type" , "application/x-www-form-urlencoded" ); |
061 |
HttpConn.setRequestProperty( "Content-Length" ,
String.valueOf(str.getBytes().length)); |
062 |
OutputStream
os = HttpConn.getOutputStream(); |
063 |
os.write(str.getBytes()); |
064 |
if (HttpConn.getResponseCode()
== HttpURLConnection.HTTP_OK) { |
065 |
return true ; |
066 |
} |
067 |
return false ; |
068 |
} |
069 |
070 |
/** |
071 |
*
以HttpClient的DoGet方式向服务器发送请数据 |
072 |
*
@param map 传递进来的数据,以map的形式进行了封装 |
073 |
*
@param path 要求服务器servlet的地址 |
074 |
*
@return 返回的boolean类型的参数 |
075 |
*
@throws Exception |
076 |
*/ |
077 |
public Boolean
submitDataByHttpClientDoGet(Map<String, String> map, String path) throws Exception
{ |
078 |
HttpClient
hc = new DefaultHttpClient(); |
079 |
//
请求路径 |
080 |
StringBuilder
sb = new StringBuilder(path); |
081 |
sb.append( "?" ); |
082 |
for (Map.Entry<String,
String> entry : map.entrySet()) { |
083 |
sb.append(entry.getKey()).append( "=" ).append(entry.getValue()); |
084 |
sb.append( "&" ); |
085 |
} |
086 |
sb.deleteCharAt(sb.length()
- 1 ); |
087 |
String
str = sb.toString(); |
088 |
System.out.println(str); |
089 |
HttpGet
request = new HttpGet(sb.toString()); |
090 |
091 |
HttpResponse
response = hc.execute(request); |
092 |
if (response.getStatusLine().getStatusCode()
== HttpURLConnection.HTTP_OK) { |
093 |
return true ; |
094 |
} |
095 |
return false ; |
096 |
} |
097 |
|
098 |
/** |
099 |
*
以HttpClient的DoPost方式提交数据到服务器 |
100 |
*
@param map 传递进来的数据,以map的形式进行了封装 |
101 |
*
@param path 要求服务器servlet的地址 |
102 |
*
@return 返回的boolean类型的参数 |
103 |
*
@throws Exception |
104 |
*/ |
105 |
public Boolean
submintDataByHttpClientDoPost(Map<String, String> map, String path) throws Exception
{ |
106 |
//
1. 获得一个相当于浏览器对象HttpClient,使用这个接口的实现类来创建对象,DefaultHttpClient |
107 |
HttpClient
hc = new DefaultHttpClient(); |
108 |
//
DoPost方式请求的时候设置请求,关键是路径 |
109 |
HttpPost
request = new HttpPost(path); |
110 |
//
2. 为请求设置请求参数,也即是将要上传到web服务器上的参数 |
111 |
List<NameValuePair>
parameters = new ArrayList<NameValuePair>(); |
112 |
for (Map.Entry<String,
String> entry : map.entrySet()) { |
113 |
NameValuePair
nameValuePairs = new BasicNameValuePair(entry.getKey(),
entry.getValue()); |
114 |
parameters.add(nameValuePairs); |
115 |
} |
116 |
//
请求实体HttpEntity也是一个接口,我们用它的实现类UrlEncodedFormEntity来创建对象,注意后面一个String类型的参数是用来指定编码的 |
117 |
HttpEntity
entity = new UrlEncodedFormEntity(parameters, "UTF-8" ); |
118 |
request.setEntity(entity); |
119 |
//
3. 执行请求 |
120 |
HttpResponse
response = hc.execute(request); |
121 |
//
4. 通过返回码来判断请求成功与否 |
122 |
if (response.getStatusLine().getStatusCode()
== HttpURLConnection.HTTP_OK) { |
123 |
return true ; |
124 |
} |
125 |
return false ; |
126 |
} |
127 |
} |