Http请求和服务器返回解析Word格式文档下载.docx
- 文档编号:15366174
- 上传时间:2022-10-29
- 格式:DOCX
- 页数:20
- 大小:46.75KB
Http请求和服务器返回解析Word格式文档下载.docx
《Http请求和服务器返回解析Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《Http请求和服务器返回解析Word格式文档下载.docx(20页珍藏版)》请在冰豆网上搜索。
viewplaincopytoclipboardprint?
1.package
com.httppost.main;
2.
3.import
java.io.IOException;
4.import
java.io.InterruptedIOException;
5.import
java.io.UnsupportedEncodingException;
6.import
java.util.ArrayList;
7.import
java.util.List;
8.
9.import
org.apache.http.HttpEntity;
10.import
org.apache.http.HttpResponse;
11.import
org.apache.http.client.ClientProtocolException;
12.import
org.apache.http.client.entity.UrlEncodedFormEntity;
13.import
org.apache.http.client.methods.HttpPost;
14.import
org.apache.http.conn.ConnectTimeoutException;
15.import
org.apache.http.impl.client.DefaultHttpClient;
16.import
org.apache.http.message.BasicNameValuePair;
17.import
org.apache.http.params.HttpConnectionParams;
18.import
org.apache.http.protocol.HTTP;
19.import
org.apache.http.util.EntityUtils;
20.
21.public
class
HttpPostRequest
{
22.
23.private
String
webContext;
24.//返回请求内容
25.public
getWebContext()
26.return
27.}
28.public
void
setWebContext(String
webContext)
29.this.webContext
=
30.}
31.//该函数返回服务器访问的各种状态,并通过webContext传递获取的文本值
32./**
33.*
参数说明
34.*
url
访问的网络地址
35.*
key
传递参数的名称
36.*
value
传递参数的值
37.*
与value数组长度对应,即一对键值对,这样可以不限制参数传递的个数
38.*
*/
39.public
int
requestHttp(String
url,String
[]key,String
[]value)
40.//
TODO
Auto-generated
method
stub
41.int
status
0;
42.DefaultHttpClient
mHttpClient
new
DefaultHttpClient();
43.HttpPost
mPost
HttpPost(url);
44.List<
BasicNameValuePair>
pairs
ArrayList<
();
45.int
size=key.length;
46.for(int
i=0;
i<
size;
i++){
47.pairs.add(new
BasicNameValuePair(key[i],
value[i]));
48.}
49.try
50.mPost.setEntity(new
UrlEncodedFormEntity(pairs,
HTTP.UTF_8));
51.}
catch
(UnsupportedEncodingException
e1)
52.//
block
53.e1.printStackTrace();
54.}
55.try
56.
//
Socket超时设置60s
57.mHttpClient.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT,60000);
58.//
连接超时60s
59.mHttpClient.getParams().setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT,60000);
60.HttpResponse
response
mHttpClient.execute(mPost);
61.int
res
response.getStatusLine().getStatusCode();
62.if
(res
==
200)
63.HttpEntity
entity
response.getEntity();
64.if
(entity
!
null)
65.String
info
EntityUtils.toString(entity);
66.setWebContext(info);
67.status=1;
68.}
69.}
else
if
404)
70.status
404;
71.}
500)
72.status
500;
73.}
74.}
(ClientProtocolException
e)
75.//
76.e.printStackTrace();
77.status
900;
78.}
(ConnectTimeoutException
79.//
80.e.printStackTrace();
81.status
901;
82.}
(InterruptedIOException
83.//
84.e.printStackTrace();
85.status
902;
86.}
(IOException
87.//
88.e.printStackTrace();
89.status
903;
90.}
91.return
status;
92.}
93.}
2.获取服务器端返回http状态的提示及处理方法
根据服务器返回的状态提示相应的信息的类,可以根据实际需要自定义提示状态,例如登录,注册等提示。
Http_Status_Tips.java源码:
android.app.ProgressDialog;
android.content.Context;
android.widget.Toast;
6.
7.public
Http_Status_Tips
8.//服务器端返回的状态提示
9.public
static
final
HTTP_0="
其他错误"
;
10.public
HTTP_1="
"
11.public
HTTP_404="
404错误,请求链接无效"
12.public
HTTP_500="
网络500错误,服务器端程序出错"
13.public
HTTP_900="
网络传输协议出错"
14.public
HTTP_901="
连接超时"
15.public
HTTP_902="
网络中断"
16.public
HTTP_903="
网络数据流传输出错"
17.public
HTTP_UNKONW="
未知的错误"
18.
19.//自定义的提示
20.private
tips;
21.
22.public
ShowHttpStatusTips(int
status,Context
context,ProgressD
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Http 求和 服务器 返回 解析
![提示](https://static.bdocx.com/images/bang_tan.gif)