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

来源:互联网 发布:三维模拟仿真软件 编辑:程序博客网 时间:2024/05/17 09:00

Volley是什么?
Google I/O 大会上,Google 推出 Volley的一个网络框架
Volley适合什么场景?
Volley适合网络通信频繁操作,并能同时实现多个网络通信。

网络框架-Volley网上很多,自己可以自行百度。我这就不提供了

1.Volley的使用解析Json

做一个简单有趣的小软件,一个QQ测凶吉的小软件,使用Volley解析一段地址获取Json并且解析Json显示出来,很简单,也很基础,不过却很直观!
先来看看效果图吧!

截图

步骤

1.申请地址已经key
2.把Volley的jar文件导入工程
3.解析地址获取到json
4.解析json填入

1.申请的Key:8d9160d4a96f2a6b5316de5b9d14d09d
2.接口地址(聚合数据申请的):http://japi.juhe.cn/qqevaluate/qq?key=你申请的Key&qq=374960286
3.将Volley导入工程
4.开工了

注意一定要先添加网络权限: 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"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.example.myapplication.MainActivity">    <RelativeLayout        android:id="@+id/tab1_rl"        android:layout_width="match_parent"        android:layout_height="51dp"        android:background="#088af7">        <TextView            android:layout_width="wrap_content"            android:layout_height="51dp"            android:layout_centerHorizontal="true"            android:background="@null"            android:gravity="center"            android:text="QQ测凶吉"            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="请输入你的QQ号码查询测试结果" />    <EditText        android:id="@+id/medittext"        android:layout_width="fill_parent"        android:layout_height="45dp"        android:layout_marginLeft="5dp"        android:layout_marginRight="5dp"        android:layout_margin="10dp"        android:background="#f7f7f7"        android:gravity="center"        android:hint="请输入正确的QQ号" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <Button            android:id="@+id/mbut"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_marginLeft="10dp"            android:layout_marginRight="10dp"            android:layout_marginTop="10dp"            android:layout_weight="1"            android:background="#088af7"            android:text="查询"            android:textColor="@android:color/white" />        <Button            android:id="@+id/mbuts"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_marginLeft="10dp"            android:layout_marginRight="10dp"            android:layout_marginTop="10dp"            android:layout_weight="1"            android:background="#088af7"            android:text="清空"            android:textColor="@android:color/white" />    </LinearLayout>    <TextView        android:layout_width="fill_parent"        android:layout_height="1dp"        android:layout_marginTop="10dp"        android:background="#aeaea9" />    <TextView        android:id="@+id/text"        android:layout_width="wrap_content"        android:layout_height="60dp"        android:layout_margin="10dp"        android:gravity="top|left"        android:textColor="@color/colorAccent"        android:text="QQ号码测试结果:" />    <TextView        android:layout_width="fill_parent"        android:layout_height="1dp"        android:background="#aeaea9" />    <TextView        android:id="@+id/mtext"        android:layout_width="wrap_content"        android:layout_height="60dp"        android:layout_margin="10dp"        android:gravity="top|left"        android:textColor="@color/colorAccent"        android:text="结果分析:" />    <TextView        android:layout_width="fill_parent"        android:layout_height="1dp"        android:background="#aeaea9" /></LinearLayout>

MainActivity.java

package com.example.myapplication;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.text.TextUtils;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;import com.android.volley.Request;import com.android.volley.RequestQueue;import com.android.volley.Response;import com.android.volley.VolleyError;import com.android.volley.toolbox.StringRequest;import com.android.volley.toolbox.Volley;import org.json.JSONException;import org.json.JSONObject;import butterknife.Bind;import butterknife.ButterKnife;import butterknife.OnClick;public class MainActivity extends AppCompatActivity {    @Bind(R.id.medittext)    EditText medittext;    @Bind(R.id.mbut)    Button mbut;    @Bind(R.id.mbuts)    Button mbuts;    @Bind(R.id.text)    TextView text;    @Bind(R.id.mtext)    TextView mtext;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ButterKnife.bind(this);    }    @OnClick({R.id.mbut, R.id.mbuts})    public void onViewClicked(View view) {        switch (view.getId()) {            case R.id.mbut:                String QQ = medittext.getText().toString();//获取输入框中QQ                if (!TextUtils.isEmpty(QQ)) {                    //解析json                    VolleyJson(QQ);                } else {                    Toast.makeText(MainActivity.this, "请输入QQ号", Toast.LENGTH_SHORT).show();                }                break;            case R.id.mbuts:                String qq = medittext.getText().toString();//获取输入框中QQ                if (!TextUtils.isEmpty(qq)){                    text.setText("QQ号码测试结果:");                    mtext.setText("结果分析:");                    medittext.setText("");                }else {                    Toast.makeText(MainActivity.this, "TM输入QQ号先", Toast.LENGTH_SHORT).show();                }                break;        }    }    //解析接口    private void VolleyJson(String QQ) {        String url = "http://japi.juhe.cn/qqevaluate/qq?key=8d9160d4a96f2a6b5316de5b9d14d09d&qq=" + QQ;        //创建请求队列        RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);        //请求        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {            @Override            public void onResponse(String s) {                getJson(s);            }        }, new Response.ErrorListener() {            @Override            public void onErrorResponse(VolleyError volleyError) {                Toast.makeText(MainActivity.this, "请查看网络", Toast.LENGTH_SHORT).show();            }        });        //添加到列队        requestQueue.add(stringRequest);    }    //json解析    private void getJson(String json) {        try {            JSONObject jsonObject = new JSONObject(json);            JSONObject jsonObject1 = jsonObject.getJSONObject("result");            JSONObject jsonObject2 = jsonObject1.getJSONObject("data");            text.setText("QQ号码测试结果:" + jsonObject2.getString("conclusion"));            mtext.setText("结果分析:" + jsonObject2.getString("analysis"));        } catch (JSONException e) {            e.printStackTrace();        }    }}

这样子就可以解析出json中的字符串并且显示出来QQ号的查询效果了,以上是这个项目的完整代码

2.Volley加载网络图片

还是从简单的做起,还是以一个例子来,先看下效果图!

这里写图片描述

步骤

1.获取图片的链接
2.添加权限
3.加载网络图片

activity_image_loader.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.example.volleydemo.ImageLoaders">    <RelativeLayout        android:id="@+id/tab1_rl"        android:layout_width="match_parent"        android:layout_height="51dp"        android:background="#088af7"  >        <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/mbutto"        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="#088af7"        android:text="加载图片" />    <ImageView        android:layout_marginTop="50dp"        android:layout_gravity="center_horizontal"        android:id="@+id/mimg"        android:layout_width="250dp"        android:layout_height="200dp"        />    <com.android.volley.toolbox.NetworkImageView        android:id="@+id/mNetwork"        android:layout_width="match_parent"        android:layout_height="200dp" /></LinearLayout>

ImageLoaders.java

package com.example.volleydemo;import android.graphics.Bitmap;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.ImageView;import com.android.volley.RequestQueue;import com.android.volley.toolbox.ImageLoader;import com.android.volley.toolbox.NetworkImageView;import com.android.volley.toolbox.Volley;import butterknife.Bind;import butterknife.ButterKnife;import butterknife.OnClick;public class ImageLoaders extends AppCompatActivity {    @Bind(R.id.mimg)    ImageView mimg;    @Bind(R.id.mbutto)    Button mbutto;    @Bind(R.id.mNetwork)    NetworkImageView mNetwork;    private String url = "http://img5.duitang.com/uploads/item/201508/24/20150824213254_aFSGE.jpeg";    private String imgurl = "http://img3.imgtn.bdimg.com/it/u=4224673892,2874807827&fm=26&gp=0.jpg";    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_image_loader);        ButterKnife.bind(this);    }    /*    * ImageListener三个参数    *     第一个 成功绑定的imgview控件    *     第二个 加载中图片    *     第三个 加载失败代替图片    *    * */    @OnClick({R.id.mbutto})    public void onViewClicked(View view) {        switch (view.getId()) {            case R.id.mbutto:                //创建请求列队                RequestQueue requestQueue = Volley.newRequestQueue(ImageLoaders.this);                //创建ImageLoader对象                ImageLoader imageLoader = new ImageLoader(requestQueue, new ImageLoader.ImageCache() {                    //空的磁盘缓存                    @Override                    public Bitmap getBitmap(String s) {                        return null;                    }                    @Override                    public void putBitmap(String s, Bitmap bitmap) {                    }                });                //实现解析                ImageLoader.ImageListener imageListener = imageLoader.getImageListener(mimg, R.mipmap.ic_launcher, R.mipmap.ic_launcher);                //请求解析                imageLoader.get(url, imageListener);                break;        }        /*        *        * 无需按钮运行就加载图片        *        * */        //创建请求列队        RequestQueue requestQueues = Volley.newRequestQueue(ImageLoaders.this);        //创建ImageLoader对象        ImageLoader imageLoaders = new ImageLoader(requestQueues, new ImageLoader.ImageCache() {            //空的磁盘缓存            @Override            public Bitmap getBitmap(String s) {                return null;            }            @Override            public void putBitmap(String s, Bitmap bitmap) {            }        });        //正在加载的图片        mNetwork.setDefaultImageResId(R.mipmap.ic_launcher);        //加载失败的图片        mNetwork.setErrorImageResId(R.mipmap.ic_launcher);        //加载图片        mNetwork.setImageUrl(imgurl,imageLoaders);    }}

这个项目只是自己的一个学习,为了自己以后观看和回忆,我只是入门级一个菜鸟,大神不要喷 - . -

阅读全文
0 0
原创粉丝点击