日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Google官方网络框架-Volley的使用解析Json以及加载网络图片方法

發布時間:2023/12/20 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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() {@Overridepublic 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:22a6ba14995ce26dd0002216be51dabbString 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>() {// 成功@Overridepublic void onResponse(String json) {Volley_Json(json);Toast.makeText(MainActivity.this, "成功:"+json, 1).show();}}, new Response.ErrorListener() {// 失敗@Overridepublic 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 blocke.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;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);getActionBar().hide();setContentView(R.layout.activity_main);initView();btn.setOnClickListener(new OnClickListener() {@Overridepublic 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>() {// 成功@Overridepublic void onResponse(String json) {Volley_Json(json);Toast.makeText(MainActivity.this, "成功:"+json, 1).show();}}, new Response.ErrorListener() {// 失敗@Overridepublic 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 blocke.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;@Overrideprotected 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() {@Overridepublic 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>() {@Overridepublic void onResponse(Bitmap response) {//成功就直接設置獲取到的bitmap圖片iv.setImageBitmap(response);}}, 0, 0, Config.RGB_565, new Response.ErrorListener() {@Overridepublic 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以及加载网络图片方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。