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

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

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

使用代码如下:

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"
tools:context="com.zzw.testvolley.MainActivity" > <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"
android:text="@string/hello_world" /> <ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" /> </LinearLayout>

activity_main.xml

MainActivity.java:

 package com.zzw.testvolley;

 import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley; import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.view.MenuItem;
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);
TextView textView = (TextView) findViewById(R.id.textView);
//设置下拉
textView.setMovementMethod(ScrollingMovementMethod.getInstance()); ImageView imageView = (ImageView) findViewById(R.id.imageView); useVolleyGetString("http://www.cnblogs.com/zzw1994", textView);
useVolleyGetImage("http://pic.cnblogs.com/avatar/822717/20151120000857.png", imageView); } private void useVolleyGetString(String url, TextView textView) { final TextView mTextView = textView; // 第一步,得到Volley请求
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); // 第二步,得到StringRequest
StringRequest stringRequest = new StringRequest(url, new Listener<String>() {
// 请求成功后返回的数据设置
@Override
public void onResponse(String response) {
mTextView.setText(response);
}
}, new ErrorListener() {
// 请求失败后返回的数据设置
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "加载失败", 1).show();
}
}); // 第三步,添加到requestQueue
requestQueue.add(stringRequest);
} /*
* 加载图片建议使用Glide
* Glide:http://www.cnblogs.com/zzw1994/p/4978312.html
*/ private void useVolleyGetImage(String url, ImageView imageView) { final ImageView mImageView = imageView; // 第一步,得到Volley请求
RequestQueue requestQuene = Volley.newRequestQueue(getApplicationContext()); // 第二步,得到ImageRequest
ImageRequest imageRequest = new ImageRequest(url,
// 请求成功后返回的数据设置
new Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
mImageView.setImageBitmap(response);
}
}, 0, 0, Config.RGB_565, new ErrorListener() {
// 请求失败后返回的数据设置
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "加载失败", 1).show();
}
}); // 第三步,添加到requestQuene
requestQuene.add(imageRequest);
}
}

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

 <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

强大的网络通信框架(实现缓存)--第三方开源--volley的更多相关文章

  1. 强大的网络通信框架(不实现缓存)--第三方开源--AsyncHttpClient

    AsyncHttpClient是一款比较流行的Android异步网路加载库,在github上的网址是:https://github.com/loopj/android-async-http但是Asyn ...

  2. 图片加载与缓存利器(自动缓存)--第三方开源-- Glide

    Android Glide使用便利,短短几行简单明晰的代码,即可完成大多数图片从网络(或者本地)加载.显示的功能需求. 使用Android Glide,需要先下载Android Glide的库,And ...

  3. IOS-常用第三方开源框架介绍

    iOS开发-常用第三方开源框架介绍(你了解的ios只是冰山一角) 时间:2015-05-06 16:43:34      阅读:533      评论:0      收藏:0      [点我收藏+] ...

  4. iOS开发-常用第三方开源框架介绍

    iOS开发-常用第三方开源框架介绍 图像: 1.图片浏览控件MWPhotoBrowser        实现了一个照片浏览器类似 iOS 自带的相册应用,可显示来自手机的图片或者是网络图片,可自动从网 ...

  5. iOS常用第三方开源框架和优秀开发者博客等

    博客收藏iOS开发过程好的开源框架.开源项目.Xcode工具插件.Mac软件.文章等,会不断更新维护,希望对你们有帮助.如果有推荐或者建议,请到此处提交推荐或者联系我. 该文档已提交GitHub,点击 ...

  6. 开源框架】Android之史上最全最简单最有用的第三方开源库收集整理,有助于快速开发

    [原][开源框架]Android之史上最全最简单最有用的第三方开源库收集整理,有助于快速开发,欢迎各位... 时间 2015-01-05 10:08:18 我是程序猿,我为自己代言 原文  http: ...

  7. 【开源框架】Android之史上最全最简单最有用的第三方开源库收集整理,有助于快速开发,欢迎各位网友补充完善

    链接地址:http://www.tuicool.com/articles/jyA3MrU 时间 2015-01-05 10:08:18  我是程序猿,我为自己代言 原文  http://blog.cs ...

  8. Core第三方开源Web框架

    NET Core第三方开源Web框架YOYOFx   YOYOFx框架 YOYOFx是一个轻量级用于构建基于 HTTP 的 Web 服务,基于 .NET 和 Mono 平台. 本着学习的态度,造了这个 ...

  9. Android-Volley网络通信框架(二次封装数据请求和图片请求(包含处理请求队列和图片缓存))

    1.回想 上篇 使用 Volley 的 JsonObjectRequest 和 ImageLoader 写了 电影列表的样例 2.重点 (1)封装Volley 内部 请求 类(请求队列,数据请求,图片 ...

随机推荐

  1. java自定义注解注解方法、类、属性等等【转】

    http://anole1982.iteye.com/blog/1450421 http://www.open-open.com/doc/view/51fe76de67214563b20b385320 ...

  2. 001 The Hello World In Csharp

    C#是面向对象编程语言,语法和JAVA非常相似.接下来让我们看一下C#的Hello world. //001.cs using System; public class Hello { public ...

  3. Android开发-API指南-进程与线程

    Processes and Threads 英文原文:http://developer.android.com/guide/components/processes-and-threads.html ...

  4. MWC飞控增加声纳定高的方法

    MWC飞控增加声纳定高的方法 2015.12.17 更新:经过2个周末的上机测试,该算法效果很好,在低空超声锁高之后离地高度非常稳定,现在已经成功应用在低空航拍上了. 现状 MWC开源飞控已经很有点年 ...

  5. 慕课网-安卓工程师初养成-1-3 使用记事本编写Java程序

    来源:http://www.imooc.com/video/1501 step1: myProgram.java    源文件       使用记事本编辑 step2: compiler        ...

  6. MSP430F149学习之路——时钟2

    代码一: /************************** 功能:LED每隔1秒闪烁一次 ****************************/ #include <msp430x14 ...

  7. 华为OJ平台——整数的二进制中1的个数

    题目描述: 输入一个整数,求该整数的二进制表达中有多少个1.例如输入10,由于其二进制表示为1010,有两个1,因此输出2. 思路: 这是一道很基本的考查位运算的面试题.包括微软在内的很多公司都曾采用 ...

  8. 可视化数据包分析工具-CapAnalysis

    可视化数据包分析工具-CapAnalysis 我们知道,Xplico是一个从pcap文件中解析出IP流量数据的工具,本文介绍又一款实用工具-CapAnalysis(可视化数据包分析工具),将比Xpli ...

  9. 【MVC】ASP.NET MVC 请求生命周期

    当一个asp.net mvc应用程序提出请求,为了响应请求,包含一些请求执行流程步骤! 在asp.net mvc应用程序Http request和Http response 过程中,主要包含8个步骤: ...

  10. css框模型

    元素的背景是内容.内边距和边框区的背景. css中:width 和 height 指的是内容区域的宽度和高度.增加内边距.边框和外边距不会影响内容区域的尺寸,但是会增加元素框的总尺寸. bootstr ...