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 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进行打印
    }});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值