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. C# Winform常见的Editor

    常见Editor: 1)ArrayEditor,继承自CollectionEditor 2)BinaryEditor 3)CollectionEditor 4)DateTimeEditor 5)Mul ...

  2. [POJ 2586] Y2K Accounting Bug (贪心)

    题目链接:http://poj.org/problem?id=2586 题目大意:(真难读懂啊)给你两个数,s,d,意思是MS公司每个月可能赚钱,也可能赔钱,如果赚钱的话,就是赚s元,如果赔钱的话,就 ...

  3. ListView设置setFooterDividersEnabled无效的原因

    参考文章:http://gundumw100.iteye.com/blog/1169065 我的情况: 高度设置为了wrap_content, 且外边有一个FrameLayout(只包含了listvi ...

  4. 自定义View的基本流程

    1.明确需求,确定你想实现的效果2.确定是使用组合控件的形式还是全新自定义的形式,组合控件即使用多个系统控件来合成一个新控件,你比如titilebar,这种形式相对简单,参考:http://blog. ...

  5. Linux系统自启动脚本

    只需编辑/etc/init.d/rc.local文件,在最后加上你的脚本即可.比如:我已经编写了一个脚本shell.sh,存放在/home/mars704/Desktop/ 下面在终端输入 gedit ...

  6. UDKtoUE4Tool-UDKUE3资源移植UE4工具

    UDKtoUE4Tool UDKtoUE4Tool 是一个把UE3/UDK资源包(T3D格式)转换成UE4(T3D格式)的工具.作者Matt3D使用C#实现,未来考虑发布到Unreal Marketp ...

  7. CCJ PRML Study Note - Chapter 1.6 : Information Theory

    Chapter 1.6 : Information Theory     Chapter 1.6 : Information Theory Christopher M. Bishop, PRML, C ...

  8. c# 获取excel所有工作表

    var filePath="f:\xx.xlsx" string connStr = "Provider=Microsoft.Ace.OleDb.12.0;" ...

  9. oracle创建索引后sqlldr导入错误

    SQL*Loader-: Error calling once/load initialization ORA-: Table TABLE_LOG has index defined upon it. ...

  10. Javascript中的Bind 、Call和Apply

    看以下代码: var bind = Function.prototype.call.bind(Function.prototype.bind); 第一眼看上去,我能猜出它究竟是用来做什么的.它把x.y ...