0
点赞
收藏
分享

微信扫一扫

android-async-http-master开源框架

书呆鱼 2023-03-16 阅读 90


开源项目链接

用于请求服务器比如上传文件下载文件android-async-http-master   就是可以下载上传文件图片等比如:

package com.li.asyny;

import java.io.File;
import java.io.FileNotFoundException;

import org.apache.http.Header;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

private EditText mEditText1;
private EditText mEditText2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}
//get请求方法
public void PostClick(View v) throws FileNotFoundException{
String url="http://10.140.0.186:8080/upload/upload";
AsyncHttpClient client=new AsyncHttpClient();
RequestParams params =new RequestParams();
params.put("fName", "phone.png");
File file=new File(getFilesDir(),"a8.png");
params.put("file", file);
//ResponseHandlerInterface
client.post(url,params, new TextHttpResponseHandler() {
//200 响应吗 调用
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
Toast.makeText(MainActivity.this, responseString, 0).show();
}
//404 500..掉用
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

}
});
}
}

 

​​Android​​-async-http仓库:​​Git​​ clone ​​https://github.com/loopj/android-async-http​​

 

android-async-http主页:​​http://loopj.com/android-async-http/​​

如下就是android-async-http的特性:

  1. 发送异步http请求,在匿名callback对象中处理response信息;
  2. http请求发生在UI(主)线程之外的异步线程中;
  3. 内部采用线程池来处理并发请求;
  4. 通过RequestParams类构造GET/POST;
  5. 内置多部分文件上传,不需要第三方库支持;
  6. 流式Json上传,不需要额外的库;
  7. 能处理环行和相对重定向;
  8. 和你的app大小相比来说,库的size很小,所有的一切只有90kb;
  9. 在各种各样的移动连接环境中具备自动智能请求重试机制;
  10. 自动的gzip响应解码;
  11. 内置多种形式的响应解析,有原生的字节流,string,json对象,甚至可以将response写到文件中;
  12. 永久的cookie保存,内部实现用的是Android的SharedPreferences;
  13. 通过BaseJsonHttpResponseHandler和各种json库集成;
  14. 支持SAX解析器;
  15. 支持各种语言和content编码,不仅仅是UTF-8;

整体操作流程

android-async-http最简单基础的使用只需如下步骤:

  1. 创建一个AsyncHttpClient;
  2. (可选的)通过RequestParams对象设置请求参数;
  3. 调用AsyncHttpClient的某个get方法,传递你需要的(成功和失败时)callback接口实现,一般都是匿名内部类,实现了AsyncHttpResponseHandler,类库自己也提供许多现成的response handler,你一般不需要自己创建。

AsyncHttpClient与AsyncHttpResponseHandler基础GET体验

AsyncHttpClient类通常用在android应用程序中创建异步GET, POST, PUT和DELETE HTTP请求,请求参数通过RequestParams实例创建,响应通过重写匿名内部类ResponseHandlerInterface方法处理。

如下代码展示了使用AsyncHttpClient与AsyncHttpResponseHandler的基础操作:

AsyncHttpClient client = new AsyncHttpClient();  

client.get("www.baidu.com", new AsyncHttpResponseHandler() {

@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

}

@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

}

@Override
public void onStart() {
super.onStart();
}

@Override
public void onFinish() {
super.onFinish();
}

@Override
public void onRetry(int retryNo) {
super.onRetry(retryNo);
}

@Override
public void onCancel() {
super.onCancel();
}

@Override
public void onProgress(int bytesWritten, int totalSize) {
super.onProgress(bytesWritten, totalSize);
}
});

 

官方推荐AsyncHttpClient静态实例化的封装

注意:官方推荐使用一个静态的AsyncHttpClient,官方示例代码如下:

public class TwitterRestClient {  
private static final String BASE_URL = "http://api.twitter.com/1/";
private static AsyncHttpClient client = new AsyncHttpClient();

public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client.get(getAbsoluteUrl(url), params, responseHandler);
}

public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client.post(getAbsoluteUrl(url), params, responseHandler);
}

private static String getAbsoluteUrl(String relativeUrl) {
return BASE_URL + relativeUrl;
}
}


 通过官方这个推荐例子可以发现,我们在用时可以直接通过类名调用需要的请求方法。所以我们可以自己多封装一些不同的请求方法,比如参数不同的方法,下载方法,上传方法等。


 

RequestParams的基础使用

RequestParams params = new RequestParams();  
params.put("username", "yanbober");
params.put("password", "123456");
params.put("email", "yanbobersky@email.com");

/*
* Upload a File
*/
params.put("file_pic", new File("test.jpg"));
params.put("file_inputStream", inputStream);
params.put("file_bytes", new ByteArrayInputStream(bytes));

/*
* url params: "user[first_name]=jesse&user[last_name]=yan"
*/
Map<String, String> map = new HashMap<String, String>();
map.put("first_name", "jesse");
map.put("last_name", "yan");
params.put("user", map);

/*
* url params: "what=haha&like=wowo"
*/
Set<String> set = new HashSet<String>();
set.add("haha");
set.add("wowo");
params.put("what", set);

/*
* url params: "languages[]=Java&languages[]=C"
*/
List<String> list = new ArrayList<String>();
list.add("Java");
list.add("C");
params.put("languages", list);

/*
* url params: "colors[]=blue&colors[]=yellow"
*/
String[] colors = { "blue", "yellow" };
params.put("colors", colors);

/*
* url params: "users[][age]=30&users[][gender]=male&users[][age]=25&users[][gender]=female"
*/
List<Map<String, String>> listOfMaps = new ArrayList<Map<String, String>>();
Map<String, String> user1 = new HashMap<String, String>();
user1.put("age", "30");
user1.put("gender", "male");

Map<String, String> user2 = new HashMap<String, String>();
user2.put("age", "25");
user2.put("gender", "female");

listOfMaps.add(user1);
listOfMaps.add(user2);

params.put("users", listOfMaps);

/*
* 使用实例
*/
AsyncHttpClient client = new AsyncHttpClient();
client.post("http://localhost:8080/androidtest/", params, responseHandler);


 


JsonHttpResponseHandler带Json参数的POST

try {  
JSONObject jsonObject = new JSONObject();
jsonObject.put("username", "ryantang");
StringEntity stringEntity = new StringEntity(jsonObject.toString());
client.post(mContext, "http://api.com/login", stringEntity, "application/json", new JsonHttpResponseHandler(){
@Override
public void onSuccess(JSONObject jsonObject) {
super.onSuccess(jsonObject);
}
});
} catch (JSONException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}


 


 BinaryHttpResponseHandler下载文件

client.get("http://download/file/test.java", new BinaryHttpResponseHandler() {  
@Override
public void onSuccess(byte[] arg0) {
super.onSuccess(arg0);
File file = Environment.getExternalStorageDirectory();
File file2 = new File(file, "down");
file2.mkdir();
file2 = new File(file2, "down_file.jpg");
try {
FileOutputStream oStream = new FileOutputStream(file2);
oStream.write(arg0);
oStream.flush();
oStream.close();
} catch (Exception e) {
e.printStackTrace();
Log.i(null, e.toString());
}
}
});

RequestParams上传文件

File myFile = new File("/sdcard/test.java");  
RequestParams params = new RequestParams();
try {
params.put("filename", myFile);
AsyncHttpClient client = new AsyncHttpClient();
client.post("http://update/server/location/", params, new AsyncHttpResponseHandler(){
@Override
public void onSuccess(int statusCode, String content) {
super.onSuccess(statusCode, content);
}
});
} catch(FileNotFoundException e) {
e.printStackTrace();
}

PersistentCookieStore持久化存储cookie

官方文档里说PersistentCookieStore类用于实现Apache HttpClient的CookieStore接口,可自动将cookie保存到Android设备的SharedPreferences中,如果你打算使用cookie来管理验证会话,这个非常有用,因为用户可以保持登录状态,不管关闭还是重新打开你的app。

文档里介绍了持久化Cookie的步骤:

  1. 创建 AsyncHttpClient实例对象;
  2. 将客户端的cookie保存到PersistentCookieStore实例对象,带有activity或者应用程序context的构造方法;
  3. 任何从服务器端获取的cookie都会持久化存储到myCookieStore中,添加一个cookie到存储中,只需要构造一个新的cookie对象,并且调用addCookie方法;

 

下面这个例子就是铁证:

AsyncHttpClient client = new AsyncHttpClient();   
PersistentCookieStore cookieStore = new PersistentCookieStore(this);
client.setCookieStore(cookieStore);

BasicClientCookie newCookie = new BasicClientCookie("name", "value");
newCookie.setVersion(1);
newCookie.setDomain("mycompany.com");
newCookie.setPath("/");
cookieStore.addCookie(newCookie);

总结性

AsyncHttpResponseHandler是一个请求返回处理、成功、失败、开始、完成等自定义的消息的类,如上第一个基础例子中所示。

BinaryHttpResponseHandler是继承AsyncHttpResponseHandler的子类,这是一个字节流返回处理的类,用于处理图片等类。

JsonHttpResponseHandler是继承AsyncHttpResponseHandler的子类,这是一个json请求返回处理服务器与客户端用json交流时使用的类。

AsyncHttpRequest继承自Runnable,是基于线程的子类,用于异步请求类, 通过AsyncHttpResponseHandler回调。

PersistentCookieStore继承自CookieStore,是一个基于CookieStore的子类, 使用HttpClient处理数据,并且使用cookie持久性存储接口。

举报

相关推荐

0 条评论