zoukankan      html  css  js  c++  java
  • Volley的三种基本用法StringRequest的Get和post用法以及JsonObjectRequest

    首先做出整个应用的全局请求队列

     1 package com.qg.lizhanqi.myvolleydemo;
     2 
     3 import android.app.Application;
     4 
     5         import com.android.volley.RequestQueue;
     6         import com.android.volley.toolbox.HttpStack;
     7         import com.android.volley.toolbox.Volley;
     8 /**
     9  * Created by lizhanqi on 2016-7-27-0027.
    10  */
    11 
    12 /**
    13  * 这是一个本应用全局的Volley请求队列,所以这里继承了Application
    14  * 由于这是一个自定义的全局的application,所以在清单文件application中加入属性
    15  *   android:name=".MyApplication"
    16  */
    17 public class MyApplication extends Application {
    18     public static RequestQueue queues;
    19 
    20     @Override
    21     public void onCreate() {
    22         super.onCreate();
    23         //实例化全局的请求队列
    24         queues = Volley.newRequestQueue(getApplicationContext(), (HttpStack) null);
    25     }
    26     public static RequestQueue getHttpQueues() {
    27         return queues;
    28     }
    29 }

    然后是StringRequest的GET请求方式

     1     private void volleyGetStringMonth(String url) {
     2          /**
     3          *  StringRequest(int method, String url, Listener<String> listener, ErrorListener errorListener)
     4          *   method请求方法 url请求路径,Listener请求成功的监听的回调,ErrorListener请求失败的监听回调
     5          */
     6         StringRequest sr = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
     7             @Override
     8             public void onResponse(String s) {
     9                 Log.i(TAG, "onResponse: 成功了");
    10                 Toast.makeText(MainActivity.this, "请求成功volleyGetStringMonth" + s, Toast.LENGTH_SHORT).show();
    11             }
    12         }, new Response.ErrorListener() {
    13             @Override
    14             public void onErrorResponse(VolleyError volleyError) {
    15                 Log.i(TAG, "onErrorResponse: 失败了");
    16             }
    17         });
    18         //设置请求标签用于加入全局队列后,方便找到
    19         sr.setTag("stringquest");
    20         //添加到全局的请求队列
    21         MyApplication.getHttpQueues().add(sr);
    22     }

    接着是StringRequest的POST请求方式的使用

     1     private void volleyPostStringMonth(String url) {
     2         StringRequest postsr = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
     3             @Override
     4             public void onResponse(String s) {
     5                 Log.i(TAG, "onResponse: 成功了");
     6                 Toast.makeText(MainActivity.this, "volleyPostStringMonth请求成功:" + s, Toast.LENGTH_SHORT).show();
     7             }
     8         }, new Response.ErrorListener() {
     9             @Override
    10             public void onErrorResponse(VolleyError volleyError) {
    11                 Log.i(TAG, "onErrorResponse: 失败了");
    12             }
    13         }) {
    14             @Override
    15             protected Map<String, String> getParams() throws AuthFailureError {
    16                 //创建一个集合,放的是keyvalue的key是参数名与value是参数值
    17                 Map<String, String> map = new HashMap<String, String>();
    18                 map.put("type", "news");
    19                 map.put("nums", "20");
    20                 return map;
    21             }
    22 
    23 
    24         };
    25         //设置请求标签用于加入全局队列后,方便找到
    26         postsr.setTag("postsr");
    27         //添加到全局的请求队列
    28         MyApplication.getHttpQueues().add(postsr);
    29     }

    最后是JsonObjectRequest的简单使用

     1         private void volleyGetJsonMonth(String url) {
     2             /*
     3             * JsonObjectRequest(int method, String url, JSONObject jsonRequest, Listener<JSONObject> listener, ErrorListener errorListener)
     4             method 请求方式
     5             url请求路径
     6              */
     7             JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
     8                 @Override
     9                 public void onResponse(JSONObject jsonObject) {
    10                     Log.i(TAG, "onResponse: JsonObjectRequest成功了");
    11                     Toast.makeText(MainActivity.this, "volleyGetJsonMonth请求成功:" + jsonObject, Toast.LENGTH_SHORT).show();
    12                 }
    13             }, new Response.ErrorListener() {
    14                 @Override
    15                 public void onErrorResponse(VolleyError volleyError) {
    16                     Log.i(TAG, "onErrorResponse: JsonObjectRequest失败了");
    17                 }
    18             });
    19             jsonObjectRequest.setTag("jsonObjectRequest");
    20             MyApplication.getHttpQueues().add(jsonObjectRequest);
    21         }

    然后我们还可以重写onstop不可见后就取消请求

    1     @Override
    2     protected void onStop() {
    3         MyApplication.getHttpQueues().cancelAll("stringquest");
    4         MyApplication.getHttpQueues().cancelAll("jsonObjectRequest");
    5         MyApplication.getHttpQueues().cancelAll("postsr");
    6         super.onStop();
    7     }
  • 相关阅读:
    HTML-图片和多媒体
    HTML弹性布局
    HTML定位——绝对定位和相对定位、固定定位
    HTML定位和布局----float浮动
    CSS层叠
    HTML-css样式引用方式
    认识HTML和CSS
    原生js 进度条
    原生js文字滚动 滚动条
    时间轴
  • 原文地址:https://www.cnblogs.com/lizhanqi/p/5712581.html
Copyright © 2011-2022 走看看