常用网络访问方式的模板
来源:互联网 发布:java一对一聊天程序 编辑:程序博客网 时间:2024/06/06 02:47
注:先加权限 internet
------1--HttpClient方式向服务器发起----GET请求-------------
//step1. 起线程
new Thread(){
public void run() {
try {
//step2. 创建能够进行网络访问的对象(HttpClient对象/HttpURLConnection对象)
HttpClient client = new DefaultHttpClient();
//step3. 设定网络访问的方式(GET/POST)
HttpGet get = new HttpGet("http://172.60.20.168:8080/ems/getCode.do");
//step4. 设定参数
//step5. 发起真正的请求,获得响应
HttpResponse resp = client.execute(get);
//step6. 解析响应内容
HttpEntity entity = resp.getEntity();
InputStream is = entity.getContent();
Bitmap bitmap = BitmapFactory.decodeStream(is);
is.close();
//step7. 将内容提交到主线程中显示
Message.obtain(handler, 101, bitmap).sendToTarget();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
};
}.start();
-----------------------2-URL的方式向服务器发起请求-----GET---------
//起一个线程
new Thread(){
public void run() {
try {
//step2. 创建能够进行网络访问的对象(HttpClient对象/HttpURLConnection对象)
URL url = new URL("http://172.60.20.168:8080/ems/getCode.do");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//step3. 设定网络访问的方式(GET/POST)
connection.setRequestMethod("GET");
connection.setDoInput(true);
//step4. 设定参数
//step5. 发起真正的请求,获得响应
connection.connect();
InputStream is = connection.getInputStream();
//step6. 解析响应内容
Bitmap bitmap = BitmapFactory.decodeStream(is);
is.close();
//step7. 将内容提交到主线程中显示
Message.obtain(handler, 101, bitmap).sendToTarget();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
};
}.start();
------3--HttpClient方式向服务器发起请求------POST--提交数据---
//step1. 起线程
new Thread(){
public void run() {
try {
//step2. 创建能够进行网络访问的对象(HttpClient对象/HttpURLConnection对象)
HttpClient client = new DefaultHttpClient();
//step3. 设定网络访问的方式(GET/POST)
HttpPost post = new HttpPost("http://172.60.20.168:8080/ems/regist.do");
//step4. 设定参数
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("loginname","laowang"));
parameters.add(new BasicNameValuePair("password","123456"));
parameters.add(new BasicNameValuePair("realname","wang"));
parameters.add(new BasicNameValuePair("email","wang@abc.com"));
HttpEntity entity = new UrlEncodedFormEntity(parameters);
post.setEntity(entity);
//step5. 发起真正的请求,获得响应
HttpResponse resp = client.execute(post);
//step6. 解析响应内容
HttpEntity respEntity = resp.getEntity();
InputStream in = respEntity.getContent();
BufferedReader br = new BufferedReader(
new InputStreamReader(in));
String result = br.readLine();
br.close();
//step7. 将内容提交到主线程中显示
Message.obtain(handler, 102, result).sendToTarget();
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
----------------------4-URL的方式向服务器发起请求-----POST---------
//step1. 起线程
new Thread(){
public void run() {
try {
//step2. 创建能够进行网络访问的对象(HttpClient对象/HttpURLConnection对象)
URL url = new URL("http://172.60.20.168:8080/ems/regist.do");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//step3. 设定网络访问的方式(GET/POST)
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//step4. 设定参数
connection.connect();
OutputStream out = connection.getOutputStream();
PrintWriter pw = new PrintWriter(out);
String params="";
HashMap<String, String> map = new HashMap<String, String>();
map.put("loginname", "laozhang");
map.put("password", "654321");
map.put("realname", "zhang");
map.put("email", "zhang@bbc.com");
params = getParams(map);
pw.print(params);
pw.flush();
pw.close();
//step5. 发起真正的请求,获得响应
InputStream in = connection.getInputStream();
BufferedReader br = new BufferedReader(
new InputStreamReader(in));
//step6. 解析响应内容
String result = br.readLine();
br.close();
//step7. 将内容提交到主线程中显示
Message.obtain(handler, 102, result).sendToTarget();
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
------1--HttpClient方式向服务器发起----GET请求-------------
//step1. 起线程
new Thread(){
public void run() {
try {
//step2. 创建能够进行网络访问的对象(HttpClient对象/HttpURLConnection对象)
HttpClient client = new DefaultHttpClient();
//step3. 设定网络访问的方式(GET/POST)
HttpGet get = new HttpGet("http://172.60.20.168:8080/ems/getCode.do");
//step4. 设定参数
//step5. 发起真正的请求,获得响应
HttpResponse resp = client.execute(get);
//step6. 解析响应内容
HttpEntity entity = resp.getEntity();
InputStream is = entity.getContent();
Bitmap bitmap = BitmapFactory.decodeStream(is);
is.close();
//step7. 将内容提交到主线程中显示
Message.obtain(handler, 101, bitmap).sendToTarget();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
};
}.start();
-----------------------2-URL的方式向服务器发起请求-----GET---------
//起一个线程
new Thread(){
public void run() {
try {
//step2. 创建能够进行网络访问的对象(HttpClient对象/HttpURLConnection对象)
URL url = new URL("http://172.60.20.168:8080/ems/getCode.do");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//step3. 设定网络访问的方式(GET/POST)
connection.setRequestMethod("GET");
connection.setDoInput(true);
//step4. 设定参数
//step5. 发起真正的请求,获得响应
connection.connect();
InputStream is = connection.getInputStream();
//step6. 解析响应内容
Bitmap bitmap = BitmapFactory.decodeStream(is);
is.close();
//step7. 将内容提交到主线程中显示
Message.obtain(handler, 101, bitmap).sendToTarget();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
};
}.start();
------3--HttpClient方式向服务器发起请求------POST--提交数据---
//step1. 起线程
new Thread(){
public void run() {
try {
//step2. 创建能够进行网络访问的对象(HttpClient对象/HttpURLConnection对象)
HttpClient client = new DefaultHttpClient();
//step3. 设定网络访问的方式(GET/POST)
HttpPost post = new HttpPost("http://172.60.20.168:8080/ems/regist.do");
//step4. 设定参数
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("loginname","laowang"));
parameters.add(new BasicNameValuePair("password","123456"));
parameters.add(new BasicNameValuePair("realname","wang"));
parameters.add(new BasicNameValuePair("email","wang@abc.com"));
HttpEntity entity = new UrlEncodedFormEntity(parameters);
post.setEntity(entity);
//step5. 发起真正的请求,获得响应
HttpResponse resp = client.execute(post);
//step6. 解析响应内容
HttpEntity respEntity = resp.getEntity();
InputStream in = respEntity.getContent();
BufferedReader br = new BufferedReader(
new InputStreamReader(in));
String result = br.readLine();
br.close();
//step7. 将内容提交到主线程中显示
Message.obtain(handler, 102, result).sendToTarget();
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
----------------------4-URL的方式向服务器发起请求-----POST---------
//step1. 起线程
new Thread(){
public void run() {
try {
//step2. 创建能够进行网络访问的对象(HttpClient对象/HttpURLConnection对象)
URL url = new URL("http://172.60.20.168:8080/ems/regist.do");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//step3. 设定网络访问的方式(GET/POST)
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//step4. 设定参数
connection.connect();
OutputStream out = connection.getOutputStream();
PrintWriter pw = new PrintWriter(out);
String params="";
HashMap<String, String> map = new HashMap<String, String>();
map.put("loginname", "laozhang");
map.put("password", "654321");
map.put("realname", "zhang");
map.put("email", "zhang@bbc.com");
params = getParams(map);
pw.print(params);
pw.flush();
pw.close();
//step5. 发起真正的请求,获得响应
InputStream in = connection.getInputStream();
BufferedReader br = new BufferedReader(
new InputStreamReader(in));
//step6. 解析响应内容
String result = br.readLine();
br.close();
//step7. 将内容提交到主线程中显示
Message.obtain(handler, 102, result).sendToTarget();
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
0 0
- 常用网络访问方式的模板
- 常用的数据库访问方式
- 常用的数据库访问方式
- Android访问网络的常用方式:Handler、AsyncTask简单例子(服务器端+Android端)
- Android访问网络的常用方式:Handler、AsyncTask简单例子(服务器端+Android端)
- Android中访问网络的方式
- 访问网络共享中的数据的方式
- 访问网络的几种方式
- Android中访问网络的方式
- HBase数据访问的一些常用方式
- android访问网络方式
- EJB 访问代理设计常用的访问方式
- 安卓访问网络常用的3种方式(httpClient, httpUrlConnection,android-query ajax)及cookie处理
- 常用的网络驱动收包方式
- android常用的网络请求方式
- 在模板类中用函数指针的方式访问其它模板类中的模板函数
- Android网络访问请求方式
- httppost方式访问网络数据
- css 学习笔记
- 大话设计模式之代理模式
- 构造MaxTree
- 机器学习之期望最大算法(EM算法)
- 第四周项目四(3)
- 常用网络访问方式的模板
- 两端移动输出一个字符串
- BP神经网络
- 读书笔记 | 从0到1:开启商业与未来的秘密
- 了解STL库中的squeue
- 设计模式之模板方法模式和策略模式
- fragment
- MIT研发的焦糖电池:未来手机将获更长续航和更轻薄的机身
- Comparable与Comparator的区别