Volley工具包第一种Set方法

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值