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呈现了一些影 ...
随机推荐
- 【环境配置】配置maven
Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具. Maven 除了以程序构建能力为特色之外,还提供高级项目管理工具.由于 Maven 的缺 ...
- 应付模块的R12 TRACE 和 FND Debug 文件 / FND 日志 调试
取得R12 TRACE: 1. 导航职责: 系统管理员> 配置文件> 系统> 查找 用户: 用户提交报表 配置: 初始化 SQL 语句 - 自定义 2. 点击用户栏位-编辑区域 ...
- 由源代码编译SpriteBuilder最新版本1.5.0搭配最新的Cocos2D 3.4.9
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 大家知道SpriteBuilder版本停留在1.4.9已经很久 ...
- Android触摸屏幕时间-android学习之旅(三)
android的多点触摸是经常遇到的编程技巧,这一篇可以将详细的介绍这个问题. 简单实例 android的触摸需要实现OnTouchListener接口,继承里面方法. 布局代码: <?xml ...
- oracle对大对象类型操作:blob,clob,nclob
1.基本介绍 Oracle和plsql都支持lob(large object) 类型,用来存储大数量数据,如图像文件,声音文件等.Oracle 9i realse2支持存储最大为4g的数据,or ...
- Android的数字选择器NumberPicker-android学习之旅(三十七)
我想说的话 今天晚上我依然在图书馆写博客,其实此刻我的没心激动而忐忑,因为明天就是足球赛的决赛,我作为主力球员压力很大,因对对方很强大,但是那又怎么样.so what...我不会停止写博客的 Numb ...
- HTML超链接之伪类注意事项
今天在复习html相关知识的时候发现了一个很常用,却经常被人们所忽略的知识点.那就是超链接伪类的使用.下面我就直接用代码来说明这一切. 伪类的相关代码 <!DOCTYPE html> &l ...
- iOS开发之一:入门介绍
今天就介绍一下iOS开发的基本的东西,有很多东西都是经常用到的而我却经常记不住,所以还是写下来吧. iOS开发需要的开发工具是Xcode,而Xcode又必须运行在 OS X(苹果系统)环境下,所以我们 ...
- 【shell脚本】nginx每天自动切割日志脚本
nginx每天日志量比较大的时候,最好每天自动切割,存储,这样可以方面以后的查询和分析 #!/bin/sh ################### #filename: nginx_log_rotat ...
- iOS中 WGAFN_网络监控 技术分享
需要用到第三方AFNetworking/SVProgressHUD 没有的可以关注我微博私信我.http://weibo.com/hanjunqiang AppDelegate.m #import & ...