基于HTTP协议的网络访问
来源:互联网 发布:caffe怎么用model 编辑:程序博客网 时间:2024/06/05 10:53
方法有两种:
一、HttpClient (apache开发)
二、HttpURLConnection(google在发布安卓时在Java基础上修改得到的)
两种途径的共同步骤:
0. 申请权限 INTERNET访问权限
1. 任何网络访问的相关代码,必须在工作线程中执行!
2. 创建HC/UC对象
3. 声明发起网络访问的方式(GET/POST)
4. 进行网络连接
5. 获得服务器响应的结果
6. 解析结果,提取需要的内容
7. 解析结果要提交到UI线程进行呈现
用两种起工作线程的方法访问网络的实例参考:
No1:
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://xxx/ems/regist.do");
//添加一个请求头,对请求实体中的参数做一个说明
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
//在post中添加请求参数
//请求参数会添加在请求实体中
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("loginname", user.getName()));
parameters.add(new BasicNameValuePair("password", user.getPassword()));
parameters.add(new BasicNameValuePair("realname", user.getRealname()));
parameters.add(new BasicNameValuePair("email", user.getEmail()));
HttpEntity entity = new UrlEncodedFormEntity(parameters);
post.setEntity(entity);
HttpResponse resp = client.execute(post);
HttpEntity respEntity = resp.getEntity();
//InputStream is = respEntity.getContent();
//
// BufferedReader br = new BufferedReader(new InputStreamReader(is));
//
// String line = br.readLine();
//
// br.close();
// HttpClient的福利类EntityUtils
String line = EntityUtils.toString(respEntity);
return line;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String result) {
listener.onRegistFinish(result);
};
}.execute();
No2:
new Thread(){
public void run() {
try {
URL url = new URL("http://xxxems/regist.do");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoInput(true);//接收服务器响应的内容
connection.setDoOutput(true);//要向服务器提交内容
//在请求头中,为请求实体中的内容做说明
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.connect();
//客户端向服务器提交参数
OutputStream out = connection.getOutputStream();
PrintWriter pw = new PrintWriter(out, true);
//String params = "loginname="+user.getName()+"&password="+user.getPassword()+"&realname="
// +user.getRealname()+"&email="+user.getEmail();
String params = getParams(user);//getParams(user)的作用同上,
pw.print(params);
pw.close();
//客户端获取服务器的响应内容
InputStream in = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
final String result = br.readLine();
br.close();
//在主线程执行
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
listener.onRegistFinish(result);
}
});
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
- 基于HTTP协议的网络访问
- 一个基于http协议的访问网络的封装类
- Android中进行基于 HTTP协议的网络访问
- 安卓中进行基于HTTP协议的网络访问
- Android中进行基于HTTP协议的网络访问基础
- HTTP 协议访问网络
- Http协议访问网络
- 安卓中进行基于HTTP协议的网络访问的两种方式
- Android中基于HTTP协议的网络访问的两种方式
- 安卓中进行基于Http协议的网络访问基础总结-1
- 安卓中进行基于Http协议的网络访问基础总结-2
- 基于Http协议的Android网络编程
- 基于Http协议的Android网络编程
- 基于Http协议的Android网络编程
- 基于Http协议的Android网络编程
- Android Http协议访问网络
- 使用HTTP协议访问网络
- 使用HTTP协议访问网络
- 错误记录--The import XXX cannot be resolved
- Eclipse Debug 的source not found问题
- jboss 7.1 的数据库配置文件
- 小议技术领域的精分化——从《找你妹》背后的游戏云聊起
- Spring Autowired(required = false) 说明
- 基于HTTP协议的网络访问
- js验证图片大小及后缀名
- 这是一篇个人博客
- jsp技术(内置对象,动作标签,指令)
- 23种设计模式再回顾
- eclipse中mahout源码的导入及编译
- 自己动手实现http协议(看透spring mvc笔记)
- makefile tutorial
- 浏览谷歌网站的方法