原文出处:http://www.open-open.com/lib/view/open1337137595541.html
Android应用开发中,会经常要提交数据到服务器和从服务器得到数据,本文主要是给出了利用http协议采用HttpClient方式向服务器提交数据的方法。
代码比较简单,这里不去过多的阐述,直接看代码。
003 |
*
本类封装了Android中向web服务器提交数据的两种方式四种方法 |
005 |
public class SubmitDataByHttpClientAndOrdinaryWay
{ |
009 |
*
@param map 传递进来的数据,以map的形式进行了封装 |
010 |
*
@param path 要求服务器servlet的地址 |
011 |
*
@return 返回的boolean类型的参数 |
014 |
public Boolean
submitDataByDoGet(Map<String, String> map, String path) throws Exception
{ |
016 |
StringBuilder
sb = new StringBuilder(path); |
018 |
for (Map.Entry<String,
String> entry : map.entrySet()) { |
019 |
sb.append(entry.getKey()).append( "=" ).append(entry.getValue()); |
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 ); |
030 |
if (HttpConn.getResponseCode()
== HttpURLConnection.HTTP_OK) { |
038 |
*
@param map 传递进来的数据,以map的形式进行了封装 |
039 |
*
@param path 要求服务器servlet的地址 |
040 |
*
@return 返回的boolean类型的参数 |
043 |
public Boolean
submitDataByDoPost(Map<String, String> map, String path) throws Exception
{ |
045 |
URL
Url = new URL(path); |
047 |
StringBuilder
sb = new StringBuilder(); |
049 |
for (Map.Entry<String,
String> entry : map.entrySet()) { |
050 |
sb.append(entry.getKey()).append( "=" ).append(entry.getValue()); |
053 |
sb.deleteCharAt(sb.length()
- 1 ); |
054 |
String
str = sb.toString(); |
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) { |
071 |
*
以HttpClient的DoGet方式向服务器发送请数据 |
072 |
*
@param map 传递进来的数据,以map的形式进行了封装 |
073 |
*
@param path 要求服务器servlet的地址 |
074 |
*
@return 返回的boolean类型的参数 |
077 |
public Boolean
submitDataByHttpClientDoGet(Map<String, String> map, String path) throws Exception
{ |
078 |
HttpClient
hc = new DefaultHttpClient(); |
080 |
StringBuilder
sb = new StringBuilder(path); |
082 |
for (Map.Entry<String,
String> entry : map.entrySet()) { |
083 |
sb.append(entry.getKey()).append( "=" ).append(entry.getValue()); |
086 |
sb.deleteCharAt(sb.length()
- 1 ); |
087 |
String
str = sb.toString(); |
088 |
System.out.println(str); |
089 |
HttpGet
request = new HttpGet(sb.toString()); |
091 |
HttpResponse
response = hc.execute(request); |
092 |
if (response.getStatusLine().getStatusCode()
== HttpURLConnection.HTTP_OK) { |
099 |
*
以HttpClient的DoPost方式提交数据到服务器 |
100 |
*
@param map 传递进来的数据,以map的形式进行了封装 |
101 |
*
@param path 要求服务器servlet的地址 |
102 |
*
@return 返回的boolean类型的参数 |
105 |
public Boolean
submintDataByHttpClientDoPost(Map<String, String> map, String path) throws Exception
{ |
107 |
HttpClient
hc = new DefaultHttpClient(); |
109 |
HttpPost
request = new HttpPost(path); |
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); |
117 |
HttpEntity
entity = new UrlEncodedFormEntity(parameters, "UTF-8" ); |
118 |
request.setEntity(entity); |
120 |
HttpResponse
response = hc.execute(request); |
122 |
if (response.getStatusLine().getStatusCode()
== HttpURLConnection.HTTP_OK) { |