强大的网络通信框架(不实现缓存)--第三方开源--AsyncHttpClient
2015-11-23 18:11
621 查看
AsyncHttpClient是一款比较流行的Android异步网路加载库,在github上的网址是:https://github.com/loopj/android-async-http
但是AsyncHttpClient和另外比较著名的Android异步网络库 Volley和Glide不同的是,不像Volley和Glide内部已经实现好了缓存策略,AsyncHttpClient自身没有实现缓存策略。
实验可以表明这点。
实验方法:我写一个简单的Android小程序,仅仅加载一张从网络获取的图片(我的CSDN博客头像),在联网时候启动这个程序,然后关闭手机设备的所以网络连接,此时再次启动该程序。
先给出测试的程序所有代码:
测试的主Activity:MainActivity.java
activity_main.xml:
activity_main.xml
最后不要忘记加权限:
但是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
最后不要忘记加权限:
<uses-permission android:name="android.permission.INTERNET" />
相关文章推荐
- Http Invoker Service & Client
- socket和TCP/IP,http的区别
- HTTP请求过程
- 利用第三方异步网路加载库AsyncHttpClient
- Asynchttpclient开源框架下载图片和文本,于Volley和Glide开源框架的区别。
- 解决ubuntu上ifconfig没有eth0且无法上网的问题
- HTTP协议版本介绍
- 开源框架 Asynchttpclient
- AsyncHttpClient 加载图片
- Android第三方异步网路加载库AsyncHttpClient
- http轮询服务器
- 网络连接与重传策略
- http://www.open-open.com/
- http://www.open-open.com/
- HTTP报文
- Win7系统设备管理器里没有网络适配器的原因及解决方法
- 网络舆情分析技术 读书笔记1
- Linux(CentOS)网络流量实时监控(iftop)
- 基础网络学习反馈(仅作新手参考)
- 《STL系列》之vector原理及实现 http://www.cnblogs.com/hlxs/p/3737687.html