强大的网络通信框架(不实现缓存)--第三方开源--AsyncHttpClient
来源:互联网 发布:中国域名价格排行 编辑:程序博客网 时间:2024/06/11 12:32
AsyncHttpClient是一款比较流行的Android异步网路加载库,在github上的网址是:https://github.com/loopj/android-async-http
但是AsyncHttpClient和另外比较著名的Android异步网络库 Volley和Glide不同的是,不像Volley和Glide内部已经实现好了缓存策略,AsyncHttpClient自身没有实现缓存策略。
实验可以表明这点。
实验方法:我写一个简单的Android小程序,仅仅加载一张从网络获取的图片(我的CSDN博客头像),在联网时候启动这个程序,然后关闭手机设备的所以网络连接,此时再次启动该程序。
先给出测试的程序所有代码:
但是AsyncHttpClient和另外比较著名的Android异步网络库 Volley和Glide不同的是,不像Volley和Glide内部已经实现好了缓存策略,AsyncHttpClient自身没有实现缓存策略。
实验可以表明这点。
实验方法:我写一个简单的Android小程序,仅仅加载一张从网络获取的图片(我的CSDN博客头像),在联网时候启动这个程序,然后关闭手机设备的所以网络连接,此时再次启动该程序。
先给出测试的程序所有代码:
测试的主Activity:MainActivity.java
package com.zzw.testasynchttpclient;import org.apache.http.Header;import com.loopj.android.http.AsyncHttpClient;import com.loopj.android.http.AsyncHttpResponseHandler;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.text.method.ScrollingMovementMethod;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView imageView = (ImageView) findViewById(R.id.imageView); TextView textView = (TextView) findViewById(R.id.textView); // 设置下拉 textView.setMovementMethod(ScrollingMovementMethod.getInstance()); useAsyncHttpClientGetString("http://www.cnblogs.com/zzw1994", textView); useAsyncHttpClientGetImage("http://pic.cnblogs.com/avatar/822717/20151120000857.png", imageView); } private void useAsyncHttpClientGetString(String url, TextView textView) { final TextView mTextView = textView; // 第一步,得到AsyncHttpClient对象 AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); // 第二步,通过url获取数据 asyncHttpClient.get(url, new AsyncHttpResponseHandler() { // 成功后返回的数据处理 @Override public void onSuccess(int statusCode, Header[] headers, byte[] response) { mTextView.setText(new String(response)); } // 失败后返回的数据处理 @Override public void onFailure(int statusCode, Header[] headers, byte[] response, Throwable error) { Toast.makeText(getApplicationContext(), "请求失败", 1).show(); } }); } private void useAsyncHttpClientGetImage(String url, ImageView imageView) { final ImageView mImageView = imageView; // 第一步,得到AsyncHttpClient对象 AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); // 第二步,通过url获取数据 asyncHttpClient.get(url, new AsyncHttpResponseHandler() { // 成功后返回的数据处理 @Override public void onSuccess(int statusCode, Header[] headers, byte[] response) { BitmapFactory bitmapFactor = new BitmapFactory(); Bitmap bitmap = bitmapFactor.decodeByteArray(response, 0, response.length); mImageView.setImageBitmap(bitmap); } // 失败后返回的数据处理 @Override public void onFailure(int statusCode, Header[] headers, byte[] response, Throwable error) { Toast.makeText(getApplicationContext(), "加载失败", 1).show(); } }); }}
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:scrollbars="vertical" android:singleLine="false" /> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" /></LinearLayout>activity_main.xml
最后不要忘记加权限:
1 <uses-permission android:name="android.permission.INTERNET" />
0 0
- 强大的网络通信框架(不实现缓存)--第三方开源--AsyncHttpClient
- 强大的网络通信框架(实现缓存)--第三方开源--volley
- 强大的请求网络图片并自动缓存的第三方框架------Picasso
- Picasso 强大的网络图片缓存框架
- Android网络请求的第三库AsyncHttpclient
- 网络请求框架AsyncHttpclient的简单使用
- Android网络框架AsyncHttpClient的二次封装
- android网络框架asyncHttpClient 的二次封装
- 部分网络框架的讲解 如:AsyncHttpClient.....
- Android-AsyncHttpClient网络框架
- 网络通信第三方框架
- Android网络请求框架--AsyncHttpClient
- 网络请求框架(一):AsyncHttpClient
- 使用AsyncHttpClient框架来完成网络数据的请求
- Android网络编程(下)--- 框架AsyncHttpClient/上传文件
- Android网络请求框架AsyncHttpClient详解(整理)
- Android第三方异步网路加载库AsyncHttpClient内部实现缓存策略了吗?
- 6.3 强大的网络请求第三方框架 Retrofit 的介绍和使用<未完待翻译>
- JVM 二
- 等宽字体
- D3学习记录之画柱状图
- JavaScript Promise启示录
- 判断是否是一个邮箱
- 强大的网络通信框架(不实现缓存)--第三方开源--AsyncHttpClient
- 【ArcGis for javascript从零开始】之三 散点聚合
- iOS strong,retain,assign,weak,copy属性实测
- jquery 最常用知识点汇总
- PowerDesigner V16.5 安装文件 及 破解文件
- nginx 多站点配置方法集合
- iOS开发CADisplayLink和NSTimer
- OC_07_01 NSData
- android 游戏总结2