Volley介绍JSon 和提交字符串参数的封装(StringRequest和Gson)

接口回调:


public interface StringVolleyCallback {
    //StringFromNet()和StringFromAndPutNet()方法调用
    public void getStringFromVolley(String s);
    //StringFromAndPutNet()方法调用
    public void putStringFromVolley(Map<String, String> map);
    //对json的字符串进行解析
    public void getJsonFromJsonString(Object o);
    //输出异常信息
    public void getJsonErrorResponse(String s);
}

方法类:


/**
 * 
 * 从网络获取字符串数据
 */

public class DataOfString {
    private Context context;
    private String url;
    private RequestQueue mQueue;
    private Class<?>  mClass;
    private int method;
    private Gson gson;
    //接口回调步骤2:定义接口成员变量
    private StringVolleyCallback stringVolleyCallback;
    //接口回调步骤3:实例化接口
    public void setVolleyCallBck(StringVolleyCallback stringVolleyCallback) {
        this.stringVolleyCallback = stringVolleyCallback;
    }

    public DataOfString(Context context, String url,int method,Class<?>  mClass){
        this.context = context;
        this.url = url;
        this.method = method;
        this.mClass = mClass;
        mQueue = Volley.newRequestQueue(context);

        GsonBuilder builder = new GsonBuilder();
        builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
            public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
                return new Date(json.getAsJsonPrimitive().getAsLong());
            }
        });
       gson = builder.create();
    }

    public DataOfString(View view,String url,int method,Class<?>  mClass){
        this(view.getContext(),url,method,mClass);
    }

    public void StringFromNet() {
        StringRequest stringRequest = new StringRequest(method,url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                //接口回调步骤4:实现接口回调方法
                if (stringVolleyCallback !=null){
                    stringVolleyCallback.getStringFromVolley(s);
                    stringVolleyCallback.getJsonFromJsonString(gson.fromJson(s,mClass));
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                //打印错误信息
                Log.e("请求错误信息:", volleyError.getMessage(), volleyError);
                if (stringVolleyCallback !=null){
                   stringVolleyCallback.getJsonErrorResponse(volleyError.getMessage());
                }
            }
        }){
            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                //获取session
                new ConnectViaSession().getSession(response);
                return super.parseNetworkResponse(response);
            }
        };

        mQueue.add(stringRequest);
    }

    public void StringFromAndPutNet() {
        StringRequest stringRequest = new StringRequest(method, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                if (stringVolleyCallback !=null){
                    stringVolleyCallback.getStringFromVolley(s);
                    stringVolleyCallback.getJsonFromJsonString(gson.fromJson(s,mClass));
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                //打印错误信息
                Log.e("TAG", volleyError.getMessage(), volleyError);
            }
        }){
            //发送session
             @Override
             public Map<String, String> getHeaders() throws AuthFailureError {
                 //发送session
                 String sesseionid = new ConnectViaSession().GetSession();
                 if (!sesseionid.equals("")) {
                     HashMap<String, String> headers = new HashMap<String, String>();
                     headers.put("cookie", sesseionid);
                     return headers;
                 }else {
                     return super.getHeaders();
              }
           }

            //给服务器提交参数
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                if (stringVolleyCallback !=null){
                    stringVolleyCallback.putStringFromVolley(map);
                    return map;
                }
                return map;
            }
        };
        mQueue.add(stringRequest);
    }
}

使用:

DataOfString json = new DataOfString(getApplicationContext(),url, Request.Method.POST,loginPojo.class);
        json.setVolleyCallBck(new StringVolleyCallback() {
            @Override
            public void getStringFromVolley(String s) {
                //System.out.println(s);
            }

            @Override
            public void putStringFromVolley(Map<String, String> map) {
       
            }

            @Override
            public void getJsonFromJsonString(Object o) {
                 
                 }
            }

            @Override
            public void getJsonErrorResponse(String s) {

            }
        });
        json.StringFromAndPutNet();


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值