Volley的post请求

package com.bawei.demo.model.http;

import android.content.Context;

import com.android.volley.AuthFailureError;
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 com.bawei.demo.model.App;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

/**

  • 作者: 姓名

  • 日期: 2019/7/19 19:19
    */
    public class HttpUtil {
    private static HttpUtil httpUtil;
    private HttpUtil(){}
    public static HttpUtil getInstance(){
    if (httpUtil==null){
    return new HttpUtil();
    }
    return httpUtil;
    }
    public void getDate(String strUrl,final CallBack callBack,final Class bean,final HashMap map){
    RequestQueue requestQueue = Volley.newRequestQueue(App.context);
    StringRequest stringRequest = new StringRequest(Request.Method.POST, strUrl, new Response.Listener() {
    @Override
    public void onResponse(String response) {
    Gson gson = new Gson();
    B b = gson.fromJson(response,bean);
    callBack.success(b);
    }
    }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {

         }
     }){
         @Override
         protected Map getParams() throws AuthFailureError {
             return map;
         }
     };
     requestQueue.add(stringRequest);
    

    }
    public interface CallBack{
    void success(B bean);
    }
    }

你可能感兴趣的:(Volley的post请求)