zoukankan      html  css  js  c++  java
  • Asynchttpclient开源框架下载图片和文本,于Volley和Glide开源框架的区别。

    AsyncHttpClient是一款比较流行的Android异步网路加载库,在github上的网址是:https://github.com/loopj/android-async-http
    AsyncHttpClient和开源框架 Volley和Glide不同的是,不像Volley和Glide内部已经实现好了缓存策略,AsyncHttpClient自身没有实现缓存策略。

    代码如下:

     1 package com.lixu.asynchttpclient;
     2 
     3 import org.apache.http.Header;
     4 import com.loopj.android.http.AsyncHttpClient;
     5 import com.loopj.android.http.AsyncHttpResponseHandler;
     6 import android.app.Activity;
     7 import android.graphics.Bitmap;
     8 import android.graphics.BitmapFactory;
     9 import android.os.Bundle;
    10 import android.widget.ImageView;
    11 import android.widget.TextView;
    12 import android.widget.Toast;
    13 
    14 public class MainActivity extends Activity {
    15     private TextView tv;
    16     private ImageView iv;
    17 
    18     @Override
    19     protected void onCreate(Bundle savedInstanceState) {
    20         super.onCreate(savedInstanceState);
    21         setContentView(R.layout.activity_main);
    22 
    23         tv = (TextView) findViewById(R.id.tv);
    24 
    25         iv = (ImageView) findViewById(R.id.iv);
    26         // 文本和图片的网络地址
    27         String url1 = "http://www.baidu.com";
    28 
    29         String url2 = "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1183223528,3058066243&fm=116&gp=0.jpg";
    30 
    31         loadtxt(url1);
    32 
    33         loadimage(url2);
    34 
    35     }
    36 
    37     private void loadtxt(String url1) {
    38         // 创建AsyncHttpClient对象
    39         AsyncHttpClient ahc = new AsyncHttpClient();
    40         ahc.get(url1, new AsyncHttpResponseHandler() {
    41 
    42             @Override
    43             public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
    44                 Toast.makeText(getApplicationContext(), "错误!!", 0).show();
    45             }
    46 
    47             @Override
    48             public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
    49                 // 将获取到的byte[]数组转换成String字符串
    50                 tv.setText(new String(arg2));
    51             }
    52         });
    53     }
    54 
    55     private void loadimage(String url2) {
    56 
    57         AsyncHttpClient ahc = new AsyncHttpClient();
    58         ahc.get(url2, new AsyncHttpResponseHandler() {
    59 
    60             @Override
    61             public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
    62 
    63                 Toast.makeText(getApplicationContext(), "错误!!", 0).show();
    64             }
    65 
    66             @Override
    67             public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
    68                 // 创建bitmap图片工厂
    69                 BitmapFactory bf = new BitmapFactory();
    70                 // 用图片工厂将字节数组转换成bitmap图片
    71                 Bitmap bitmap = bf.decodeByteArray(arg2, 0, arg2.length);
    72 
    73                 iv.setImageBitmap(bitmap);
    74 
    75             }
    76 
    77         });
    78 
    79     }
    80 }

    xml文件:

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:id="@+id/LinearLayout1"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     android:orientation="vertical" >
     7 
     8     <ScrollView
     9         android:layout_width="match_parent"
    10         android:layout_height="match_parent"
    11         android:layout_weight="1" >
    12 
    13         <TextView
    14             android:id="@+id/tv"
    15             android:layout_width="match_parent"
    16             android:layout_height="match_parent" />
    17     </ScrollView>
    18 
    19     <ImageView
    20         android:id="@+id/iv"
    21         android:layout_width="match_parent"
    22         android:layout_height="match_parent"
    23         android:layout_weight="1" />
    24 
    25 </LinearLayout>

    运行效果:

  • 相关阅读:
    泛微协同OA制造业解决方案
    泛微协同OA房地产行业解决方案
    基于内置web工作流的政府OA解决方案
    泛微协同OA广告行业解决方案
    成功导入数据
    首页做完了
    终于知道如何才能取得HtmlEditor里的数据了
    国庆放假了!
    最近这两天
    Dictionary<string, string>是一个泛型使用说明
  • 原文地址:https://www.cnblogs.com/labixiaoxin/p/4989064.html
Copyright © 2011-2022 走看看