欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android框架Volley使用:Get请求实现

程序员文章站 2023-01-21 08:35:38
首先我们在项目中导入这个框架: 在AndroidManifest文件当中添加网络权限: 下面是我们的首页布局:在这个布局当中我们将Volley框架的所有功能都做成了一个按钮,按下按钮之后就会在“显示结果”下面显示结果,显示结果下面使用了一个ScrollView,并在ScrollView下面嵌套了一个 ......

首先我们在项目中导入这个框架:

implementation 'com.mcxiaoke.volley:library:1.0.19'

在androidmanifest文件当中添加网络权限:

<uses-permission android:name="android.permission.internet"/>

下面是我们的首页布局:
在这个布局当中我们将volley框架的所有功能都做成了一个按钮,按下按钮之后就会在“显示结果”下面显示结果,显示结果下面使用了一个scrollview,并在scrollview下面嵌套了一个textview和imageview,用于把我们加载成功之后的图片和文字进行显示。

Android框架Volley使用:Get请求实现

下面是首页布局的代码:

<?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=".mainactivity">
<button
    android:id="@+id/get"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="get请求"/>
    <button
        android:id="@+id/post"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="post请求"/>
    <button
        android:id="@+id/json"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="请求json"/>
    <button
        android:id="@+id/imagerquest"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="imagerquest加载图片"/>
    <button
        android:id="@+id/imageloader"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="imageloader加载图片"/>
    <button
        android:id="@+id/networkimageview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="networkimageview加载图片"/>
    <textview
        android:text="显示结果"
        android:textsize="20sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <imageview
        android:visibility="gone"
        android:id="@+id/iv_volley"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <com.android.volley.toolbox.networkimageview
        android:id="@+id/network"
        android:visibility="gone"
        android:layout_width="200dp"
        android:layout_height="200dp" />
   <scrollview
       android:layout_width="match_parent"
       android:layout_height="match_parent">
       <textview
           android:id="@+id/tv_volley_result"
           android:layout_width="match_parent"
           android:layout_height="match_parent" />



   </scrollview>
</linearlayout>

为了实现get请求,进行get请求一共需要三步,分别是:

  1. 创建一个请求队列
  2. 创建一个请求
  3. 将创建的请求添加到请求队列当中

在创建请求的时候,必须同时写两个监听器,一个是实现请求,正确接受数据的回调,另一个是发生异常之后的回调。这里我们准备了json数据,是在gank.io的官网上找的,大家可以自行百度一下,这里就直接采用了网址:

http://gank.io/api/xiandu/category/wow

当中的json数据进行get请求了,只要我们在文本显示区返回的数据和这个网站上面的数据显示相同,则请求成功。如果不同也会显示出错误的原因。

实现的核心代码如下:
  public void initlistener()
    {
        get.setonclicklistener(new view.onclicklistener() {
            @override
            public void onclick(view view) {
                //创建一个请求队列
                requestqueue requestqueue=volley.newrequestqueue(mainactivity.this);
                //创建一个请求
                string url="http://gank.io/api/xiandu/category/wow";
                stringrequest stringrequest=new stringrequest(url, new response.listener<string>() {
                   //正确接受数据之后的回调
                    @override
                    public void onresponse(string response) {
                    tv_volley_result.settext(response);
                    }
                }, new response.errorlistener() {//发生异常之后的监听回调
                    @override
                    public void onerrorresponse(volleyerror error) {
                        tv_volley_result.settext("加载错误"+error);
                    }
                });
                //将创建的请求添加到请求队列当中
                requestqueue.add(stringrequest);
            }
        });

全部主活动的java代码如下:

import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.view.view;
import android.widget.button;
import android.widget.imageview;
import android.widget.textview;

import com.android.volley.requestqueue;
import com.android.volley.response;
import com.android.volley.volleyerror;
import com.android.volley.toolbox.networkimageview;
import com.android.volley.toolbox.stringrequest;
import com.android.volley.toolbox.volley;

public class mainactivity extends appcompatactivity {
    private button get;
    private button post;
    private button json;
    private button imagerequest;
    private  button imageload;
    private  button networkimageview;

    private imageview iv;
    private networkimageview network;
    private textview tv_volley_result;
    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_main);
        initview();
        initlistener();

    }
    public void initview()//把需要初始化的控件的逻辑都写在这里是一个很好的编程范式
    {

        get=findviewbyid(r.id.get);
        post=findviewbyid(r.id.post);
        json=findviewbyid(r.id.json);
        imagerequest=findviewbyid(r.id.imagerquest);
        imageload=findviewbyid(r.id.imageloader);
        networkimageview=findviewbyid(r.id.networkimageview);
        iv=findviewbyid(r.id.iv_volley);
        network=findviewbyid(r.id.network);
        tv_volley_result=findviewbyid(r.id.tv_volley_result);



    }
    public void initlistener()
    {
        get.setonclicklistener(new view.onclicklistener() {
            @override
            public void onclick(view view) {
                //创建一个请求队列
                requestqueue requestqueue=volley.newrequestqueue(mainactivity.this);
                //创建一个请求
                string url="http://gank.io/api/xiandu/category/wow";
                stringrequest stringrequest=new stringrequest(url, new response.listener<string>() {
                   //正确接受数据之后的回调
                    @override
                    public void onresponse(string response) {
                    tv_volley_result.settext(response);
                    }
                }, new response.errorlistener() {//发生异常之后的监听回调
                    @override
                    public void onerrorresponse(volleyerror error) {
                        tv_volley_result.settext("加载错误"+error);
                    }
                });
                //将创建的请求添加到请求队列当中
                requestqueue.add(stringrequest);
            }
        });


        post.setonclicklistener(new view.onclicklistener() {
            @override
            public void onclick(view view) {

            }
        });

        json.setonclicklistener(new view.onclicklistener() {
            @override
            public void onclick(view view) {

            }
        });

        imagerequest.setonclicklistener(new view.onclicklistener() {
            @override
            public void onclick(view view) {

            }
        });


        imageload.setonclicklistener(new view.onclicklistener() {
            @override
            public void onclick(view view) {

            }
        });

        networkimageview.setonclicklistener(new view.onclicklistener() {
            @override
            public void onclick(view view) {

            }
        });

    }
}

运行程序,点击get按钮得到以下界面则请求成功:

Android框架Volley使用:Get请求实现

 

得解也!!厉害吧