常用网络访问方式的模板
2016-03-20 21:38
477 查看
注:先加权限 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();
相关文章推荐
- HttpFox插件安装和打开教程
- Mitmproxy 拦截、mock移动设备网络请求
- 桥接-NAT-hostonly
- 解决ISO9强制https造成已有的app中原有的http失败的问题。
- HttpHandler与HttpModule的用处与区别
- HTTP 错误 404.17 - Not Found
- 20159311《网络攻击与防范》第二周学习总结
- 20159311《网络攻击与防范》第三周学习总结
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) 二分答案 + 网络流
- Ubuntu下安装配置Lighttpd+PHP+SQLite +cgi
- BZOJ1834 [ZJOI2010] network 网络扩容
- [毕业设计]手写数字识别系统设计与实现
- boost::asio::ip::tcp::socket is connected?(如何知道socket的链接是链接或断开?)
- 虚拟机的三种网络模式
- TCP
- 网络层
- 自动化测试,HttpClient调用Rest服务
- XML数据包网络请求
- iOS学习之网络编程(1)
- Java Code Examples for org.springframework.http.HttpStatus