Volley工具包第一种Set方法

来源:互联网 发布:手机淘宝如何举报卖家 编辑:程序博客网 时间:2024/06/12 22:13

volley的工具包

package com.bwie.utils;

import android.content.Context;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

public class HttpUtil {
private String url = “http://121.42.8.95:8090/ECServer_D/“;
private Context context;
private String json=”“;
//声明接口
private VolleyCallBack callBack;
//定义接口回调的方法
public void setCallBack(VolleyCallBack callBack) {
this.callBack = callBack;
}
//定义接口
public interface VolleyCallBack{
void setDataFromVolley(String json);
}

public HttpUtil(Context context) {    super();    this.context = context;}public void Get(String path) {    RequestQueue requestQueue = Volley.newRequestQueue(context);    Request request = new StringRequest(url + path, new Listener<String>() {        @Override        public void onResponse(String response) {            // TODO Auto-generated method stub            System.out.println(response);            json = response;//调用接口将得到的结果返还回去                        callBack1.setDataFromVolley(json);        }    }, new ErrorListener() {        @Override        public void onErrorResponse(VolleyError error) {            // TODO Auto-generated method stub        }    });    requestQueue.add(request);}

}

activity的用法

httpUtil = new HttpUtil(getActivity());
httpUtil.setCallBack(new HttpUtil.VolleyCallBack(){

    @Override    public void setDataFromVolley(String json) {        // TODO Auto-generated method stub        //把json数据显示在ui界面        System.out.println(json);    }});
0 0
原创粉丝点击