各种网络请求的封装
来源:互联网 发布:网络现金赌博游戏平台 编辑:程序博客网 时间:2024/04/28 19:52
网络的各种操作结合:
package com.qianfeng.client;
importjava.io.ByteArrayOutputStream;
importjava.io.File;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.OutputStream;
importjava.io.UnsupportedEncodingException;
importjava.net.HttpURLConnection;
importjava.net.MalformedURLException;
importjava.net.URL;
importjava.util.ArrayList;
importjava.util.Iterator;
importjava.util.List;
importjava.util.Map;
importjava.util.Set;
importorg.apache.http.HttpEntity;
importorg.apache.http.HttpResponse;
importorg.apache.http.NameValuePair;
importorg.apache.http.client.ClientProtocolException;
importorg.apache.http.client.HttpClient;
importorg.apache.http.client.entity.UrlEncodedFormEntity;
importorg.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
importorg.apache.http.entity.mime.FormBodyPart;
importorg.apache.http.entity.mime.MultipartEntity;
importorg.apache.http.entity.mime.content.FileBody;
importorg.apache.http.entity.mime.content.StringBody;
importorg.apache.http.impl.client.DefaultHttpClient;
importorg.apache.http.message.BasicNameValuePair;
importorg.apache.http.util.EntityUtils;
public classHttpUtils {
//用java提供的类从服务器端获取资源
public static voidgetResource(final String path,final CallBack callBack){
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnectionconnection=null;
try {
URL url=new URL(path);
connection=(HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(2000);
connection.setDoInput(true);
connection.connect();
int code=connection.getResponseCode();
if (code==200) {
InputStreamis=connection.getInputStream();
ByteArrayOutputStreambos=new ByteArrayOutputStream();
byte[]b=newbyte[1024];
int len=-1;
while((len=is.read(b))!=-1) {
bos.write(b,0, len);
}
callBack.load(bos.toByteArray());
}
} catch (MalformedURLExceptione) {
// TODO Auto-generatedcatch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
}
}
}).start();
}
//用java提供的类往服务器端发送数据,并获得服务器端发送给客户端的数据。
public static void upDate(final Stringpath,final Map<String,String> map,final CallBackPost callBackPost){
new Thread(new Runnable() {
@Override
public void run() {
try {
URLurl=new URL(path);
HttpURLConnectionconnection=(HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setConnectTimeout(2000);
connection.setDoInput(true);
connection.setDoOutput(true);
Set<String>set=map.keySet();
Iterator<String>iterator=set.iterator();
OutputStreamos=null;
while(iterator.hasNext()) {
Stringkey=iterator.next();
Stringvalue=map.get(key);
os=connection.getOutputStream();
//Stringstr="username="+username+"&userpwd="+password;
Stringstring=key+"="+value+"&";
System.out.println(string);
os.write(string.getBytes());
}
os.close();
intcode=connection.getResponseCode();
if(code==200) {
InputStreamis=connection.getInputStream();
ByteArrayOutputStreambos=new ByteArrayOutputStream();
byte[]b=newbyte[1024];
intlen=-1;
while((len=is.read(b))!=-1) {
bos.write(b,0, len);
}
callBackPost.showMessage(bos.toByteArray());
}
} catch(MalformedURLException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch(IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
//用apache提供的类从服务器端获取资源
public static voidhttpGetResource(final String path,final CallBackHttpGet callBackHttpGet){
new Thread(new Runnable() {
@Override
public void run() {
HttpClientclient=new DefaultHttpClient();
HttpGetget=new HttpGet(path);
try {
HttpResponseresponse=client.execute(get);
intcode=response.getStatusLine().getStatusCode();
if(code==200) {
HttpEntityentity=response.getEntity();
ByteArrayOutputStreambos=new ByteArrayOutputStream();
bos.write(EntityUtils.toByteArray(entity));
callBackHttpGet.load(bos.toByteArray());
}
} catch(ClientProtocolException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch(IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
//用apache提供的类实现登录。
public static voidupHttpPostDate(final String path,final Map<String,String> map,finalCallBackHttpPost callBackHttpPost){
new Thread(new Runnable() {
@Override
public void run() {
HttpClientclient=new DefaultHttpClient();
HttpPostpost=new HttpPost(path);
List<NameValuePair>list=new ArrayList<NameValuePair>();
Set<String>set=map.keySet();
Iterator<String>iterator=set.iterator();
while(iterator.hasNext()) {
String key=iterator.next();
Stringvalue=map.get(key);
NameValuePairvaluePair=new BasicNameValuePair(key, value);
list.add(valuePair);
}
try {
HttpEntityentity=new UrlEncodedFormEntity(list);
post.setEntity(entity);
HttpResponseresponse=client.execute(post);
intcode=response.getStatusLine().getStatusCode();
if(code==200) {
HttpEntityentity2=response.getEntity();
ByteArrayOutputStreambos=new ByteArrayOutputStream();
bos.write(EntityUtils.toByteArray(entity2));
callBackHttpPost.showMessage(bos.toByteArray());
}
} catch(UnsupportedEncodingException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch(ClientProtocolException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch(IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
//用apache提供的类实现上传文件和其他的数据。
public static voidupload(final String path,final File file,final Map<String,String>map,final CallBackUpLod callBackUpLod){
new Thread(newRunnable() {
@Override
public voidrun() {
HttpClientclient=new DefaultHttpClient();
HttpPostpost=new HttpPost(path);
MultipartEntityentity=new MultipartEntity();
FileBodyfileBody=new FileBody(file);
FormBodyPartform=new FormBodyPart("form", fileBody);
Set<String>set=map.keySet();
Iterator<String>iterator=set.iterator();
while(iterator.hasNext()) {
Stringkey=iterator.next();
Stringvalue=map.get(key);
try{
entity.addPart(key,newStringBody(value));
}catch (UnsupportedEncodingException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
entity.addPart(form);
post.setEntity(entity);
try{
HttpResponseresponse=client.execute(post);
intcode=response.getStatusLine().getStatusCode();
if(code==200) {
HttpEntityentity2=response.getEntity();
ByteArrayOutputStreambos=new ByteArrayOutputStream();
bos.write(EntityUtils.toByteArray(entity2));
callBackUpLod.load(bos.toByteArray());
}
}catch (ClientProtocolException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
}
interface CallBack{
void load(byte[] b);
}
interfaceCallBackPost{
void showMessage(byte[] b );
}
interfaceCallBackHttpGet{
void load(byte[] b);
}
interfaceCallBackHttpPost{
void showMessage(byte[] b );
}
interfaceCallBackUpLod{
void load(byte[] b);
}
- 各种网络请求的封装
- LoadingLayout封装网络请求的各种效果
- 网络请求的封装
- 封装的网络请求
- 网络请求的封装
- 网络请求的封装
- 网络请求的封装方法
- HttpURLConnection网络请求的封装
- Xutils3网络请求的封装
- Xutils3网络请求的封装
- 封装OkHttp的网络请求
- 常见的Android 网络请求的封装;
- 使用NSURLConnection的网络请求与封装
- MKNetworkKit对网络请求的完美封装
- 第三方网络请求的封装
- AFNetworking 2.5.x 网络请求的封装
- 自定义UI 网络 请求类的封装
- 基于AFNetWoring 封装的网络请求类
- 举夜派饰檬厥泼寿锤载盐亲狼挡敲
- UIImageView 基本语法
- 10_Java高新_枚举-反射-注解-泛型
- :last-child works, :first-child doesn't
- Mac系统软件优化
- 各种网络请求的封装
- 训练计划
- BZOJ 1013: [JSOI2008]球形空间产生器sphere 高斯消元
- malloc和new的区别是什么?
- 菏泽牡丹区小留集的,这个社会不太平
- 概率论1--基本概念
- IOS - Xcode 6创建空应用(Empty Application)
- 使用Matlab画心形线
- 关于计算机图形学的学习