Android volley网络框架

本文详细介绍了Volley网络请求库的使用方法,包括如何初始化请求队列,发送GET和POST请求,以及如何处理响应和错误。通过具体的代码示例,展示了如何使用StringRequest和JsonObjectRequest进行网络通信。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.youjiaopingtai.api.http;

import android.content.Context;
import android.util.Log;

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.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.youjiaopingtai.api.util.LogUtils;

import org.json.JSONObject;

/**
 * Created by Administrator on 2018/8/16.
 */
public class VolleyHttp {
    private static String TAG = "VolleyHttp";
    public static RequestQueue mQueue = null;

    public static void getInstance(Context context) {
        if (mQueue == null) {
            synchronized (context) {
                mQueue = Volley.newRequestQueue(context);
            }
        }
    }

    /**
     * Get 请求
     *
     * @param url  接口地址
     * @param what 回调地址
     */
    public static void get(final HttpCallBack httpCallBack, final int what, String url) {
        LogUtils.e(TAG, url);
        StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.e(TAG, "onResponse: " + response);
                httpCallBack.onResponse(what, response.toString());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "onErrorResponse: " + error.toString());
                httpCallBack.onErrorResponse(error.toString());
            }
        });
        mQueue.add(request);
    }

    /**
     * Post 请求
     *
     * @param strurl     请求路径
     * @param jsonObject 携带参数
     * @param what       回调位置
     */
    public static void post(final HttpCallBack httpCallBack, String strurl, JSONObject jsonObject, final int what) {
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, strurl, jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        Log.e(TAG, "onResponse: " + jsonObject.toString());
                        httpCallBack.onResponse(what, jsonObject.toString());
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG, "onErrorResponse: " + volleyError.toString());
                httpCallBack.onErrorResponse(volleyError.toString());
            }
        });
        mQueue.add(jsonObjectRequest);
    }

    public interface HttpCallBack {

        /**
         * 调用接口响应成功信息
         */
        void onResponse(int what, String response);

        /**
         * 调用接口出错信息
         */
        void onErrorResponse(String error);
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落魄的Android开发

感谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值