Google官方网络框架-Volley的使用解析Json以及加载网络图片方法
Google官方网络框架-Volley的使用解析Json以及加载网络图片方法
Volley是什么?
Google I/O 大会上,Google 推出 Volley的一个网络框架
Volley适合什么场景?
Volley适合网络通信频繁操作,并能同时实现多个网络通信。
下载地址:http://download.csdn.net/detail/qq_26787115/9358787
1.Volley的使用解析Json
我们不罗嗦,直接开讲:
我们的需求很简单,就是做一个归属地查询的小软件,使用Volley解析一段地址获取Json并且解析Json显示出来,很简单的需求吧,也很基础,不过却很直观!
先来看看效果图吧!
步骤
1.申请地址已经key
2.把Volley的jar文件导入工程
3.解析地址获取到json
4.解析json填入
1.申请的Key:22a6ba14995ce26dd0002216be51dabb
2.接口地址(聚合数据申请的):http://apis.juhe.cn/mobile/get?phone=13429667914&key=您申请的KEY
3.将Volley导入工程
4.开工了
注意一定要先添加权限:<uses-permission android:name="android.permission.INTERNET"/>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/tab1_rl"
android:layout_width="match_parent"
android:layout_height="51dp"
android:background="#34c083" >
<TextView
android:layout_width="wrap_content"
android:layout_height="51dp"
android:layout_centerHorizontal="true"
android:background="@null"
android:gravity="center"
android:text="归属地查询"
android:textColor="@android:color/white"
android:textSize="20dp" />
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="请输入你的手机号码查询归属地信息" />
<EditText
android:id="@+id/et"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@drawable/ed_bg"
android:gravity="center"
android:hint="请输入正确的电话号码" />
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#34c083"
android:text="查询"
android:textColor="@android:color/white" />
<TextView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:background="#aeaea9" />
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:gravity="center_vertical"
android:text="归属地:" />
<TextView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#aeaea9" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:gravity="center_vertical"
android:text="区号:" />
<TextView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#aeaea9" />
<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:gravity="center_vertical"
android:text="运营商:" />
<TextView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#aeaea9" />
<TextView
android:id="@+id/tv4"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:gravity="center_vertical"
android:text="用户类型:" />
<TextView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#aeaea9" />
</LinearLayout>
这里没什么可说的,和预览界面的布局是一样的
EditText:输入电话号码 id: android:id="@+id/et"
Button:点击查询 android:id="@+id/btn"
TextView:归属地,区号,运营商,用户类型 android:id="@+id/tv1234"
MainActivity.java
这里首先说一下步骤了:
1.初始化这几个控件
2.给Button添加点击事件
//这里就是判断用户输入的方法,输入不为空的话就执行 Volley_Get();方法
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myPhone = et.getText().toString();
if (et == null) {
Toast.makeText(MainActivity.this, "号码不能为空",
Toast.LENGTH_LONG).show();
} else {
Volley_Get();
}
}
});
3.Volley_Get方法是解析接口获取Json字符的,并且使用的是GET方法,还有POST方法我就不赘述了,抛砖引玉,不懂自行Google
private void Volley_Get() {
//接口地址 myphone为我们输入的电话号码 Key:22a6ba14995ce26dd0002216be51dabb
String url = "http://apis.juhe.cn/mobile/get?phone=" + myPhone
+ "&key=22a6ba14995ce26dd0002216be51dabb";
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest request = new StringRequest(Method.GET, url,
new Listener<String>() {
// 成功
@Override
public void onResponse(String json) {
Volley_Json(json);
Toast.makeText(MainActivity.this, "成功:"+json, 1).show();
}
}, new Response.ErrorListener() {
// 失败
@Override
public void onErrorResponse(VolleyError errorLog) {
Toast.makeText(MainActivity.this, "失败:"+errorLog.toString(),
Toast.LENGTH_LONG).show();
}
});
queue.add(request);
}
4.Volley_Json();
当我们解析这个接口成功的话就会得到一个json的字符串了,具体的样子是这个样子的
{
"resultcode": "200",
"reason": "Return Successd!",
"result": {
"province": "江西",
"city": "吉安",
"areacode": "0796",
"zip": "343000",
"company": "中国联通",
"card": "江西联通GSM卡"
},
"error_code": 0
}
我们现在新建一个方法Volley_Json()并且定义一个String的参数,如下:
private void Volley_Json(String json) {
//result为200说明成功
try {
JSONObject jsonObject = new JSONObject(json);
JSONObject object = jsonObject.getJSONObject("result");
tv1.setText("归属地:" + object.getString("province") + "-"
+ object.getString("city"));
tv2.setText("区号:" + object.getString("areacode"));
tv3.setText("运营商:" + object.getString("company"));
tv4.setText("用户类型:" + object.getString("card"));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这样子就可以解析出json中的字符串并且显示出来达到归属地的查询效果了,下面是MainActivity的完整代码以及Demo下载链接:
package com.lgl.queryaddress;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
public class MainActivity extends Activity {
private TextView tv1, tv2, tv3, tv4;
private EditText et;
private Button btn;
private String myPhone;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getActionBar().hide();
setContentView(R.layout.activity_main);
initView();
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myPhone = et.getText().toString();
if (et == null) {
Toast.makeText(MainActivity.this, "号码不能为空",
Toast.LENGTH_LONG).show();
} else {
Volley_Get();
}
}
});
}
private void initView() {
et = (EditText) findViewById(R.id.et);
btn = (Button) findViewById(R.id.btn);
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
tv3 = (TextView) findViewById(R.id.tv3);
tv4 = (TextView) findViewById(R.id.tv4);
}
private void Volley_Get() {
String url = "http://apis.juhe.cn/mobile/get?phone=" + myPhone
+ "&key=22a6ba14995ce26dd0002216be51dabb";
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest request = new StringRequest(Method.GET, url,
new Listener<String>() {
// 成功
@Override
public void onResponse(String json) {
Volley_Json(json);
Toast.makeText(MainActivity.this, "成功:"+json, 1).show();
}
}, new Response.ErrorListener() {
// 失败
@Override
public void onErrorResponse(VolleyError errorLog) {
Toast.makeText(MainActivity.this, "失败:"+errorLog.toString(),
Toast.LENGTH_LONG).show();
}
});
queue.add(request);
}
private void Volley_Json(String json) {
//result为200说明成功
try {
JSONObject jsonObject = new JSONObject(json);
JSONObject object = jsonObject.getJSONObject("result");
tv1.setText("归属地:" + object.getString("province") + "-"
+ object.getString("city"));
tv2.setText("区号:" + object.getString("areacode"));
tv3.setText("运营商:" + object.getString("company"));
tv4.setText("用户类型:" + object.getString("card"));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Demo下载地址:http://download.csdn.net/detail/qq_26787115/9360953
2.Volley加载网络图片
相对于请求json字符串,解析网络的图片倒是步骤少了,玩法也多起来,我们还是从简单的做起,还是以一个例子来,先看下效果图!
就是一个Button和一个ImageView,点击Button加载图片信息
步骤
1.获取图片的链接
2.添加权限
3.加载网络图片
layout_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"
android:background="#FDFDFD">
<RelativeLayout
android:id="@+id/tab1_rl"
android:layout_width="match_parent"
android:layout_height="51dp"
android:background="#34c083" >
<TextView
android:layout_width="wrap_content"
android:layout_height="51dp"
android:layout_centerHorizontal="true"
android:background="@null"
android:gravity="center"
android:text="归属地查询"
android:textColor="@android:color/white"
android:textSize="20dp" />
</RelativeLayout>
<Button
android:id="@+id/btn"
android:textSize="20sp"
android:textColor="@android:color/white"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="30dp"
android:background="#34c083"
android:text="加载图片" />
<ImageView
android:layout_marginTop="50dp"
android:layout_gravity="center_horizontal"
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>
一个Button android:id="@+id/btn"
一个imageview android:id="@+id/iv"
初始化这两个控件之后就直接解析了,代码不多,看MainActivity的完整代码
package com.lglvolleyiv;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private Button btn;
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().hide();
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Volley_Iv();
}
});
}
// 加载图片
protected void Volley_Iv() {
//图片是百度的logo,直接浏览器右键获取图片地址即可
String url ="http://ss.bdimg.com/static/superman/img/logo/bd_logo1_31bdc765.png";
//请求
RequestQueue queue = Volley.newRequestQueue(this);
/**
* ImageRequest的构造函数接收六个参数,
* 第一个参数就是图片的URL地址。
* 第二个参数是图片请求成功的回调,这里我们把返回的Bitmap参数设置到ImageView中。
* 第三第四个参数分别用于指定允许图片最大的宽度和高度,设置不正确会对图片进行压缩。
* 第五个参数用于指定图片的颜色属性,Bitmap.Config下的几个常量都可以在这里使用。
* 第六个参数是图片请求失败的回调,这里我们当请求失败时在ImageView中显示一张默认图片。
*/
ImageRequest imageRequest = new ImageRequest(url, new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
//成功就直接设置获取到的bitmap图片
iv.setImageBitmap(response);
}
}, 0, 0, Config.RGB_565, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// 失败了
}
});
//最后将这个ImageRequest对象添加到RequestQueue里就可以
queue.add(imageRequest);
}
}
Demo下载地址:http://download.csdn.net/detail/qq_26787115/9362615
这个项目的完整思路应该是查询到了运营商显示相应的logo,不过这部分还没做,完善了整个项目的构架就上架了,有兴趣的可以去试试:
百度:http://shouji.baidu.com/software/item?docid=8424313&from=as
91:http://apk.91.com/Soft/Android/com.lgl.queryaddress-1.html
安卓:http://apk.hiapk.com/appinfo/com.lgl.queryaddress/1
上线项目的源码:http://download.csdn.net/detail/qq_26787115/9379019
后续还会继续更新一些,这只是抛砖引玉写一些基础而已,高手勿喷!!!
Google官方网络框架-Volley的使用解析Json以及加载网络图片方法的更多相关文章
- Google官方网络框架Volley实战——QQ吉凶测试,南无阿弥陀佛!
Google官方网络框架Volley实战--QQ吉凶测试,南无阿弥陀佛! 这次我们用第三方的接口来做一个QQ吉凶的测试项目,代码依然是比较的简单 无图无真相 直接撸代码了,详细解释都已经写在注释里了 ...
- android官方开源的高性能异步加载网络图片的Gridview例子
这个是我在安卓安卓巴士上看到的资料,放到这儿共享下.这个例子android官方提供的,其中讲解了如何异步加载网络图片,以及在gridview中高效率的显示图片此代码很好的解决了加载大量图片时,报OOM ...
- 【安卓网络请求开源框架Volley源码解析系列】定制自己的Request请求及Volley框架源码剖析
通过前面的学习我们已经掌握了Volley的基本用法,没看过的建议大家先去阅读我的博文[安卓网络请求开源框架Volley源码解析系列]初识Volley及其基本用法.如StringRequest用来请求一 ...
- Android网络框架Volley(体验篇)
Volley是Google I/O 2013推出的网络通信库,在volley推出之前我们一般会选择比较成熟的第三方网络通信库,如: android-async-http retrofit okhttp ...
- Android网络框架Volley
Volley是Google I/O 2013推出的网络通信库,在volley推出之前我们一般会选择比较成熟的第三方网络通信库,如: android-async-http retrofit okhttp ...
- Android网络框架Volley(实战篇)
之前讲了ym—— Android网络框架Volley(体验篇),大家应该了解了volley的使用,接下来我们要看看如何把volley使用到实战项目里面,我们先考虑下一些问题: 从上一篇来看 mQu ...
- Android 网络框架Volley的使用
Volley简介 在平时的开发过程中,我们的应用几乎总是在和网络打交道, 在android下的网络编程一般都是基于Http协议的 ,常见的是HttpURLConnection和HttpClient 两 ...
- Android热门网络框架Volley详解
.Volley简介 volley的英文意思为‘群发’.‘迸发’.Volley是2013年谷歌官方发布的一款Android平台上的网络通信库.Volley非常适合一些数据量不大,但需要频繁通信的网络操作 ...
- Android网络框架-Volley实践 使用Volley打造自己定义ListView
这篇文章翻译自Ravi Tamada博客中的Android Custom ListView with Image and Text using Volley 终于效果 这个ListView呈现了一些影 ...
随机推荐
- Java程序员必须掌握的线程知识-Callable和Future
Callable和Future出现的原因 创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable接口. 这2种方式都有一个缺陷就是:在执行完任务之后无法获取执行结果. 如果需 ...
- 应付模块的R12 TRACE 和 FND Debug 文件 / FND 日志 调试
取得R12 TRACE: 1. 导航职责: 系统管理员> 配置文件> 系统> 查找 用户: 用户提交报表 配置: 初始化 SQL 语句 - 自定义 2. 点击用户栏位-编辑区域 ...
- ROS_Kinetic_24 使用catkin_create_qt_pkg快速创建qt-ros功能包
使用catkin_create_qt_pkg快速创建qt-ros功能包 参考网址: qt_create:http://wiki.ros.org/qt_create qt_ros:https://git ...
- 【一天一道LeetCode】#121. Best Time to Buy and Sell Stock
# 一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say ...
- Java-IO之超类OutputStream
图中我们可以看出,以字节为单位的输出流的公共父类是OutputStream: 从中我们可以看出,以字节为单位的输出流的公共父类是OutputStream: (1)OutputStream是以字节为单位 ...
- UNIX环境高级编程——System V 共享内存区
共享内存区域是被多个进程共享的一部分物理内存.如果多个进程都把该内存区域映射到自己的虚拟地址空间,则这些进程就都可以直接访问该共享内存区域,从而可以通过该区域进行通信.共享内存是进程间共享数据的一种最 ...
- 【Linux 操作系统】 Secure CRT 终端配置 -- 配置语法高亮 光标 和 字体
. 1. Secure CRT 中没有想要的字体 Windows 8 下没有 Courier New 字体, 需要在系统的字体上进行配置, 进入 C:\Windows\Fonts 目录, 下面是目录的 ...
- Android项目-高考作文功能简介(一)
前言 : 开发安卓也已2年多了近3年了, 在自己刚入行的时候就有自己独立开发一个App的想法. 后来自己做了<<高考作文>>这一App. 后面续续断断的维护者. 也因为功能简 ...
- Android ToggleButton 实践
在android的开发过程中,对于ToggleButton的使用频率也是相当的高的,下面我就来说一下,这个组件的两种使用方式. 第一种是简单的使用,利用Toast的方式弹出提示语句 需要注意的是要想自 ...
- IT职场: 选择外企利与弊
前几天有个同学打电话问我选择国内企业与外企的利弊,很可笑的是他是学机械的:既然和我完全不在一个行业,因此我只是说了我们IT外企的利与弊,毕竟隔行如隔山. 首先简单自我介绍一下,我所在的公司是美资500 ...