• 强大的网络通信框架(实现缓存)--第三方开源--volley


    Android Volley是Android平台上很好用的第三方开源网络通信框架。使用简答,功能强大。

    Android Volley的库jar包Volley.ja下载连接地址:Volley下载

    下载后解压的volley.jar直接添加到项目的libs中就可以使用

    使用代码如下:

    activity_main.xml:

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:orientation="vertical"
     6     tools:context="com.zzw.testvolley.MainActivity" >
     7 
     8     <TextView
     9         android:id="@+id/textView"
    10         android:layout_width="match_parent"
    11         android:layout_height="wrap_content"
    12         android:layout_weight="1"
    13         android:scrollbars="vertical"
    14         android:singleLine="false"
    15         android:text="@string/hello_world" />
    16 
    17     <ImageView
    18         android:id="@+id/imageView"
    19         android:layout_width="match_parent"
    20         android:layout_height="wrap_content"
    21         android:layout_weight="1" />
    22 
    23 </LinearLayout>
    activity_main.xml

    MainActivity.java:

     1 package com.zzw.testvolley;
     2 
     3 import com.android.volley.RequestQueue;
     4 import com.android.volley.Response.ErrorListener;
     5 import com.android.volley.Response.Listener;
     6 import com.android.volley.VolleyError;
     7 import com.android.volley.toolbox.ImageRequest;
     8 import com.android.volley.toolbox.StringRequest;
     9 import com.android.volley.toolbox.Volley;
    10 
    11 import android.app.Activity;
    12 import android.graphics.Bitmap;
    13 import android.graphics.Bitmap.Config;
    14 import android.os.Bundle;
    15 import android.text.method.ScrollingMovementMethod;
    16 import android.view.Menu;
    17 import android.view.MenuItem;
    18 import android.widget.ImageView;
    19 import android.widget.TextView;
    20 import android.widget.Toast;
    21 
    22 public class MainActivity extends Activity {
    23 
    24     @Override
    25     protected void onCreate(Bundle savedInstanceState) {
    26         super.onCreate(savedInstanceState);
    27         setContentView(R.layout.activity_main);
    28         TextView textView = (TextView) findViewById(R.id.textView);
    29         //设置下拉
    30         textView.setMovementMethod(ScrollingMovementMethod.getInstance());
    31 
    32         ImageView imageView = (ImageView) findViewById(R.id.imageView);
    33 
    34         useVolleyGetString("http://www.cnblogs.com/zzw1994", textView);
    35         useVolleyGetImage("http://pic.cnblogs.com/avatar/822717/20151120000857.png", imageView);
    36 
    37     }
    38 
    39     private void useVolleyGetString(String url, TextView textView) {
    40 
    41         final TextView mTextView = textView;
    42 
    43         // 第一步,得到Volley请求
    44         RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
    45 
    46         // 第二步,得到StringRequest
    47         StringRequest stringRequest = new StringRequest(url, new Listener<String>() {
    48             // 请求成功后返回的数据设置
    49             @Override
    50             public void onResponse(String response) {
    51                 mTextView.setText(response);
    52             }
    53         }, new ErrorListener() {
    54             // 请求失败后返回的数据设置
    55             @Override
    56             public void onErrorResponse(VolleyError error) {
    57                 Toast.makeText(getApplicationContext(), "加载失败", 1).show();
    58             }
    59         });
    60 
    61         // 第三步,添加到requestQueue
    62         requestQueue.add(stringRequest);
    63     }
    64 
    65     /*
    66      * 加载图片建议使用Glide 
    67      * Glide:http://www.cnblogs.com/zzw1994/p/4978312.html
    68      */
    69 
    70     private void useVolleyGetImage(String url, ImageView imageView) {
    71 
    72         final ImageView mImageView = imageView;
    73 
    74         // 第一步,得到Volley请求
    75         RequestQueue requestQuene = Volley.newRequestQueue(getApplicationContext());
    76 
    77         // 第二步,得到ImageRequest
    78         ImageRequest imageRequest = new ImageRequest(url,
    79                 // 请求成功后返回的数据设置
    80                 new Listener<Bitmap>() {
    81                     @Override
    82                     public void onResponse(Bitmap response) {
    83                         mImageView.setImageBitmap(response);
    84                     }
    85                 }, 0, 0, Config.RGB_565, new ErrorListener() {
    86                     // 请求失败后返回的数据设置
    87                     @Override
    88                     public void onErrorResponse(VolleyError error) {
    89                         Toast.makeText(getApplicationContext(), "加载失败", 1).show();
    90                     }
    91                 });
    92 
    93         // 第三步,添加到requestQuene
    94         requestQuene.add(imageRequest);
    95     }
    96 }

    最后不要忘记在AndroidManifest.xml中添加权限:

    1 <uses-permission android:name="android.permission.INTERNET" />

     Android Volley的技术文档主页:https://developer.android.com/training/volley/index.html
    Android Volley的开源代码库官方主页:https://android.googlesource.com/platform/frameworks/volley

  • 相关阅读:
    php单元测试
    git配置本地环境(phpstudy/tortoisegit/git等)
    xp/win7中系统安装memcached服务,卸载memcached服务,以及删除memcached服务
    memcached装、启动和卸载
    如何安装memcached
    三元运算符、逻辑运算符
    移动端页面怎么适配ios页面
    javascript正则表达式——元字符
    一个div添加多个背景图片
    GNU Screen使用入门
  • 原文地址:https://www.cnblogs.com/zzw1994/p/4988712.html
Copyright © 2020-2023  润新知