volley工具包的第二种简介的方法

来源:互联网 发布:淘宝一千零一夜 编辑:程序博客网 时间:2024/06/05 19:56

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 interface VolleyCallBack{
void setDataFromVolley(String json);
}

public HttpUtil(Context context) {    super();    this.context = context;}public void Get(String path,final VolleyCallBack callBack) {    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;        //接口回调              callBack.setDataFromVolley(json);        }    }, new ErrorListener() {        @Override        public void onErrorResponse(VolleyError error) {            // TODO Auto-generated method stub        }    });    requestQueue.add(request);}

}

Activity的用法

httpUtil = new HttpUtil(getActivity());
httpUtil.Get(“home”,new VolleyCallBack(){

    @Override    public void setDataFromVolley(String json) {        // TODO Auto-generated method stub        //将得到的json进行打印    }});
0 0
原创粉丝点击