美文网首页我爱编程
volley框架使用小Demo

volley框架使用小Demo

作者: Echoooo_o | 来源:发表于2018-05-27 14:36 被阅读0次

MainActivity

public class MainActivity extends AppCompatActivity  {

private static final String URL = "https://www.baidu.com/";
private RequestQueue mQueue; // volley的请求队列
@BindView(R.id.volley_get)
Button btn;
TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mQueue = Volley.newRequestQueue(getApplicationContext());
    ButterKnife.bind(this);
}

@OnClick({R.id.volley_get})
public void onClick(View v) {
    get();
}

/**
 * 创建一个请求,这里我们做一个最简单的通过GET方式请求网页源码的操作。请求成功后打印结果。
 */
private void get() {
    StringRequest request = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {

        @Override
        public void onResponse(String arg0) {
            //Toast.makeText(getApplicationContext(), arg0, Toast.LENGTH_LONG).show();
            textView = (TextView) findViewById(R.id.text);
            textView.setText(arg0.toString());
            //Log.d("onResponse", arg0);
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError arg0) {
            //Toast.makeText(getApplicationContext(), arg0.toString(), Toast.LENGTH_LONG).show();
            textView = (TextView) findViewById(R.id.text);
            textView.setText(arg0.toString());
        }
    });
    mQueue.add(request);
}

}

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.an.volleytest.MainActivity">

<Button
    android:id="@+id/volley_get"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="get请求"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:id="@+id/text"
    android:layout_marginTop="25dp"
    android:layout_below="@+id/volley_get"
    android:layout_centerHorizontal="true" />
</RelativeLayout>
捕获.PNG

点击按钮 hello world 替换为请求网址的html文件

相关文章

  • volley框架使用小Demo

    MainActivity } activity_main.xml 点击按钮 hello world 替换为请求网址...

  • Volley

    Volley地址Volley, Volley是Google开源的一个网络框架 Demo 通过Volley.newR...

  • Android:Volley的使用和Volley源码分析

    Volley网络框架 Volley的简单使用 Volley源码分析 }

  • Volley+Gson+Picasso实现联网加载图片进行显示

    我的主页Demo下载地址 介绍之前先来看一下,使用这三个框架实现的结果。 一、Volley框架的简单使用 Voll...

  • (二十一)Volley框架面试问题

    一、Volley使用简介 Volley是谷歌官方推出的网络请求框架,适合数据量小但通信频繁的网络操作。 1.首先需...

  • Android studio 导入volley源码

    看了许多网络请求框架,最终决定使用volley。开始导入,通过git下载volley,添加为项目module,这样...

  • Volley解析

    前言 Volley是什么鬼? Volley是谷歌提出的网络通信框架,该框架封装了网络通信和图片加载,使得使用起来更...

  • Volley源码解析

    Volley作为轻量级网络请求框架已经被广泛使用,这篇文章就从源码角度深层次了解Volley的构成,立足于熟练使用...

  • Volley框架的使用

    Volley框架 Volley是Google官方出的一套小而巧的异步请求库,该框架封装的扩展性很强,支持HttpC...

  • Volley框架的使用

    环境:win10+as2.3 Volley网络框架,适用于并发、高频词、小数据的网络请求。对于文件上传和下载等不适...

网友评论

    本文标题:volley框架使用小Demo

    本文链接:https://www.haomeiwen.com/subject/szmbjftx.html