配色: 字号:
Google官方网络框架-Volley的使用解析Json以及加载网络图片方法
2016-10-11 | 阅:  转:  |  分享 
  
Google官方网络框架-Volley的使用解析Json以及加载网络图片方法

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.开工了

注意一定要先添加权限:

activity_main.xml






android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">




android:id="@+id/tab1_rl"

android:layout_width="match_parent"

android:layout_height="51dp"

android:background="#34c083">




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"/>






android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:layout_marginTop="10dp"

android:text="请输入你的手机号码查询归属地信息"/>




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="请输入正确的电话号码"/>




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"/>




android:layout_width="fill_parent"

android:layout_height="1dp"

android:layout_marginTop="10dp"

android:background="#aeaea9"/>




android:id="@+id/tv1"

android:layout_width="wrap_content"

android:layout_height="40dp"

android:gravity="center_vertical"

android:text="归属地:"/>




android:layout_width="fill_parent"

android:layout_height="1dp"

android:background="#aeaea9"/>




android:id="@+id/tv2"

android:layout_width="wrap_content"

android:layout_height="40dp"

android:gravity="center_vertical"

android:text="区号:"/>




android:layout_width="fill_parent"

android:layout_height="1dp"

android:background="#aeaea9"/>




android:id="@+id/tv3"

android:layout_width="wrap_content"

android:layout_height="40dp"

android:gravity="center_vertical"

android:text="运营商:"/>




android:layout_width="fill_parent"

android:layout_height="1dp"

android:background="#aeaea9"/>




android:id="@+id/tv4"

android:layout_width="wrap_content"

android:layout_height="40dp"

android:gravity="center_vertical"

android:text="用户类型:"/>




android:layout_width="fill_parent"

android:layout_height="1dp"

android:background="#aeaea9"/>







这里没什么可说的,和预览界面的布局是一样的

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(newOnClickListener(){



@Override

publicvoidonClick(Viewv){

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



privatevoidVolley_Get(){

//接口地址myphone为我们输入的电话号码Key:22a6ba14995ce26dd0002216be51dabb

Stringurl="http://apis.juhe.cn/mobile/get?phone="+myPhone

+"&key=22a6ba14995ce26dd0002216be51dabb";

RequestQueuequeue=Volley.newRequestQueue(this);

StringRequestrequest=newStringRequest(Method.GET,url,

newListener(){

//成功

@Override

publicvoidonResponse(Stringjson){

Volley_Json(json);

Toast.makeText(MainActivity.this,"成功:"+json,1).show();

}

},newResponse.ErrorListener(){

//失败

@Override

publicvoidonErrorResponse(VolleyErrorerrorLog){

Toast.makeText(MainActivity.this,"失败:"+errorLog.toString(),

Toast.LENGTH_LONG).show();

}

});

queue.add(request);



}



4.Volley_Json();



当我们解析这个接口成功的话就会得到一个json的字符串了,具体的样子是这个样子的

{

"resultcode":"200",

"reason":"ReturnSuccessd!",

"result":{

"province":"江西",

"city":"吉安",

"areacode":"0796",

"zip":"343000",

"company":"中国联通",

"card":"江西联通GSM卡"

},

"error_code":0

}



我们现在新建一个方法Volley_Json()并且定义一个String的参数,如下:

privatevoidVolley_Json(Stringjson){

//result为200说明成功

try{

JSONObjectjsonObject=newJSONObject(json);

JSONObjectobject=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(JSONExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

}



这样子就可以解析出json中的字符串并且显示出来达到归属地的查询效果了,下面是MainActivity的完整代码以及Demo下载链接:



packagecom.lgl.queryaddress;



importorg.json.JSONException;

importorg.json.JSONObject;



importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

importandroid.widget.EditText;

importandroid.widget.TextView;

importandroid.widget.Toast;



importcom.android.volley.Request.Method;

importcom.android.volley.RequestQueue;

importcom.android.volley.Response;

importcom.android.volley.Response.Listener;

importcom.android.volley.VolleyError;

importcom.android.volley.toolbox.StringRequest;

importcom.android.volley.toolbox.Volley;



publicclassMainActivityextendsActivity{



privateTextViewtv1,tv2,tv3,tv4;

privateEditTextet;

privateButtonbtn;

privateStringmyPhone;



@Override

protectedvoidonCreate(BundlesavedInstanceState){

//TODOAuto-generatedmethodstub

super.onCreate(savedInstanceState);

getActionBar().hide();

setContentView(R.layout.activity_main);



initView();

btn.setOnClickListener(newOnClickListener(){



@Override

publicvoidonClick(Viewv){

myPhone=et.getText().toString();

if(et==null){

Toast.makeText(MainActivity.this,"号码不能为空",

Toast.LENGTH_LONG).show();

}else{

Volley_Get();

}

}

});

}



privatevoidinitView(){

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);

}



privatevoidVolley_Get(){

Stringurl="http://apis.juhe.cn/mobile/get?phone="+myPhone

+"&key=22a6ba14995ce26dd0002216be51dabb";

RequestQueuequeue=Volley.newRequestQueue(this);

StringRequestrequest=newStringRequest(Method.GET,url,

newListener(){

//成功

@Override

publicvoidonResponse(Stringjson){

Volley_Json(json);

Toast.makeText(MainActivity.this,"成功:"+json,1).show();

}

},newResponse.ErrorListener(){

//失败

@Override

publicvoidonErrorResponse(VolleyErrorerrorLog){

Toast.makeText(MainActivity.this,"失败:"+errorLog.toString(),

Toast.LENGTH_LONG).show();

}

});

queue.add(request);



}



privatevoidVolley_Json(Stringjson){

//result为200说明成功

try{

JSONObjectjsonObject=newJSONObject(json);

JSONObjectobject=jsonObject.getJSONObject("result");

tv1.setText("归属地:"+object.getString("province")+"-"

+object.gewww.shanxiwang.nettString("city"));

tv2.setText("区号:"+object.getString("areacode"));

tv3.setText("运营商:"+object.getString("company"));

tv4.setText("用户类型:"+object.getString("card"));



}catch(JSONExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

}

}

Demo下载地址:http://download.csdn.net/detail/qq_26787115/9360953



2.Volley加载网络图片



相对于请求json字符串,解析网络的图片倒是步骤少了,玩法也多起来,我们还是从简单的做起,还是以一个例子来,先看下效果图!



就是一个Button和一个ImageView,点击Button加载图片信息



步骤

1.获取图片的链接

2.添加权限

3.加载网络图片

layout_main.xml




xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:background="#FDFDFD">




android:id="@+id/tab1_rl"

android:layout_width="match_parent"

android:layout_height="51dp"

android:background="#34c083">




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"/>






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="加载图片"/>




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"/>







一个Buttonandroid:id="@+id/btn"

一个imageviewandroid:id="@+id/iv"

初始化这两个控件之后就直接解析了,代码不多,看MainActivity的完整代码



packagecom.lglvolleyiv;



importcom.android.volley.RequestQueue;

importcom.android.volley.Response;

importcom.android.volley.VolleyError;

importcom.android.volley.toolbox.ImageRequest;

importcom.android.volley.toolbox.Volley;



importandroid.app.Activity;

importandroid.graphics.Bitmap;

importandroid.graphics.Bitmap.Config;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

importandroid.widget.ImageView;



publicclassMainActivityextendsActivity{



privateButtonbtn;

privateImageViewiv;



@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

getActionBar().hide();

setContentView(R.layout.activity_main);



iv=(ImageView)findViewById(R.id.iv);

btn=(Button)findViewById(R.id.btn);

btn.setOnClickListener(newOnClickListener(){



@Override

publicvoidonClick(Viewv){

Volley_Iv();

}

});



}



//加载图片

protectedvoidVolley_Iv(){

//图片是百度的logo,直接浏览器右键获取图片地址即可

Stringurl="http://ss.bdimg.com/static/superman/img/logo/bd_logo1_31bdc765.png";

//请求

RequestQueuequeue=Volley.newRequestQueue(this);

/

ImageRequest的构造函数接收六个参数,

第一个参数就是图片的URL地址。

第二个参数是图片请求成功的回调,这里我们把返回的Bitmap参数设置到ImageView中。

第三第四个参数分别用于指定允许图片最大的宽度和高度,设置不正确会对图片进行压缩。

第五个参数用于指定图片的颜色属性,Bitmap.Config下的几个常量都可以在这里使用。

第六个参数是图片请求失败的回调,这里我们当请求失败时在ImageView中显示一张默认图片。

/

ImageRequestimageRequest=newImageRequest(url,newResponse.Listener(){



@Override

publicvoidonResponse(Bitmapresponse){

//成功就直接设置获取到的bitmap图片

iv.setImageBitmap(response);

}

},0,0,Config.RGB_565,newResponse.ErrorListener(){



@Override

publicvoidonErrorResponse(VolleyErrorerror){

//失败了

}

});

//最后将这个ImageRequest对象添加到RequestQueue里就可以

queue.add(imageRequest);

}

}

这个项目的完整思路应该是查询到了运营商显示相应的logo,不过这部分还没做,完善了整个项目的构架就上架了.

献花(0)
+1
(本文系网络学习天...首藏)