android-async-http

本文介绍了Android异步HTTP客户端的基本用法,包括GET和POST请求的实现方式,如何处理JSON响应及文件下载,同时也提供了数据上传的例子,并简要说明了如何操作cookie。

Android Asynchronous Http Client

asyncHttpCliet 是apache http 封装后的用于网络请求数据、上传数据的异步类。

基本的用法:
创建一个AsyncHttpClient后,可以使用get的方法和post方法,分别就是对应的get和post的方式访问。

下面分别对访问和上传进行说明。

(一)一般数据访问:
AsyncHttpClient client = new AsyncHttpClient();
		try {
			RequestParams params = new RequestParams();
			params.put("keyword", URLEncoder.encode("你好", "UTF-8"));
		
			client.get("http://client.azrj.cn/json/cook/cook_list.jsp?type=1&p=2&size=10", params, new AsyncHttpResponseHandler() {
			    @Override
			    public void onSuccess(String response) {
			    	L.i(getClass(), "response:"+response);
			    }
			   
			    @Override
			    public void onStart() {
			    	super.onStart();
			    	L.i(getClass(), "onStart()");
			    }
			    
			    @Override
			    public void onFinish() {
			    	super.onFinish();
			    	L.i(getClass(), "onFinish()");
			    }
			   @Override
				public void onFailure(int arg0, Header[] arg1, byte[] arg2,
						Throwable arg3) {
				   L.i(getClass(), "onFailure()");
					super.onFailure(arg0, arg1, arg2, arg3);
				}
			});
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

Json数据访问:
只需将onSuccess函数稍作修改即可。
@Override
			    public void onSuccess(String response) {
			    	L.i(getClass(), "response:"+response);
			    	try {
					JSONObject jObject = new JSONObject(response);
					L.i(getClass(), "jObject:"+jObject.toString());
				} catch (JSONException e) {
					e.printStackTrace();
				}
			    }

File图片数据访问:
  @Override
			    public void onSuccess(int code, Header[] headers, byte[] bytes) {
			    	super.onSuccess(code, headers, bytes);
			    	File file = new File(path);
			    	FileOutputStream oStream = new FileOutputStream(file);
			    	oStream.write(bytes);
			    	oStream.flush();
			        oStream.close();
			    }

(二)一般数据上传:
设置好params就行。
RequestParams params = new RequestParams();
params.put("key", "value");
params.put("more", "data");

文件数据上传:
只需修改param里的数据即可:
方法.1  流方法
InputStream myInputStream = blah;
RequestParams params = new RequestParams();
params.put("secret_passwords", myInputStream, "passwords.txt");
方法.2  文件方法
File myFile = new File("/path/to/file.png");
RequestParams params = new RequestParams();
try {
    params.put("profile_picture", myFile);
} catch(FileNotFoundException e) {}
方法.3 字节流方法,针对数据大的如MP3
byte[] myByteArray = blah;
RequestParams params = new RequestParams();
params.put("soundtrack", new ByteArrayInputStream(myByteArray), "she-wolf.mp3");


最后简单的介绍一下async-http的cookie的操作:
绑定cookie
PersistentCookieStore myCookieStore = new PersistentCookieStore(this);
myClient.setCookieStore(myCookieStore);
设置cookie的参数
BasicClientCookie newCookie = new BasicClientCookie("cookiesare", "awesome");
newCookie.setVersion(1);
newCookie.setDomain("mydomain.com");
newCookie.setPath("/");
myCookieStore.addCookie(newCookie);

这是官网:http://loopj.com/android-async-http/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值