百度搜索:直接拿来用 最火的android开源项目
android-async-http:
An asynchronous callback-based Http client for Android built on top of Apache’s HttpClient libraries. All requests are made outside of your app’s main UI thread, but any callback logic will be executed on the same thread as the callback was created using Android’s Handler message passing. You can also use it in Service or background thread, library will automatically recognize in which context is ran.
联网取数据,以及主线程与工作线程之间的交互已经封装好了。
如何使用?
1.Add maven dependency using Gradle buildscript in format
dependencies {
compile 'com.loopj.android:android-async-http:1.4.9'
}
2.Import the http package.
import com.loopj.android.http.*;
3.Create a new AsyncHttpClient instance and make a request:
get请求:
AsyncHttpClient client = new AsyncHttpClient();
client.get("https://www.google.com", new AsyncHttpResponseHandler() {
@Override
public void onStart() {
// called before request is started
}
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] response) {
// called when response HTTP status is "200 OK"
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
// called when response HTTP status is "4XX" (eg. 401, 403, 404)
}
@Override
public void onRetry(int retryNo) {
// called when request is retried
}
});
直接封装后使用:
public class TwitterRestClient {
private static final String BASE_URL = "https://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;
}
}
android-smart-image-view:
SmartImageView作为自定义控件来使用,封装了联网load图片的操作,以及主线程与工作线程之间的交互。
SmartImageView is a drop-in replacement for Android’s standard ImageView which additionally allows images to be loaded from URLs or the user’s contact address book. Images are cached to memory and to disk for super fast loading.
如何使用?
1.在xml布局中引入控件
<com.loopj.android.image.SmartImageView
android:id="@+id/my_image" />
2.Get a reference to the layout’s SmartImageView:
SmartImageView myImage = (SmartImageView) this.findViewById(R.id.my_image);
3.Load an image into the view from a URL:(包含下载与显示)
myImage.setImageUrl("http://www.awesomeimages.com/myawesomeimage.jpg");
Load an image into the view from the phone’s contact address book:
`
myImage.setImageContact(contactAddressBookId);
`