下载volley.jar ,导入到工程中。
volley网络请求管理工具类
public class RequestManager {
private static RequestQueue mRequestQueue; //请求队列
public static void init(Context context){
mRequestQueue = Volley.newRequestQueue(context);
}
public static RequestQueue getRequestQueue() {
if (mRequestQueue != null) {
return mRequestQueue;
} else {
throw new IllegalStateException("RequestQueue not initialized");
}
}
/**
* 添加请求
* @param request
* @param tag
*/
public static void addRequest(Request> request, Object tag) {
if (tag != null) {
request.setTag(tag);
}
if(mRequestQueue!=null){
mRequestQueue.add(request);
}
}
/**
* 取消所有tag请求
* @param tag
*/
public static void cancelAll(Object tag) {
if(mRequestQueue!=null){
mRequestQueue.cancelAll(tag);
}
}
//释放资源
public static void release() {
mRequestQueue = null;
}
}
3.1. Activity基类,所有用volley请求服务器的Activity类应继承该类
public class BaseActivity extends AppCompatActivity {
protected Activity context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
}
@Override
public void onDestroy(){
super.onDestroy();
RequestManager.cancelAll(this);
}
protected void executeRequest(Request request) {
RequestManager.addRequest(request, this);
}
protected Response.ErrorListener errorListener(final Dialog mpro) {
return new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, getResources().getString(R.string.error_volley), Toast.LENGTH_LONG).show();
if(mpro != null && mpro.isShowing()){
mpro.dismiss();
}
}
};
}
}
3.2. Fragment基类,所有用volley请求服务器的Fragment类应继承该类
public class BaseFragment extends Fragment{
protected Activity context ;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
context = getActivity();
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onDestroy(){
super.onDestroy();
RequestManager.cancelAll(this);
}
protected void executeRequest(Request request) {
RequestManager.addRequest(request, this);
}
protected Response.ErrorListener errorListener(final Dialog mpro) {
return new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, getResources().getString(R.string.error_volley), Toast.LENGTH_LONG).show();
if(mpro != null && mpro.isShowing()){
mpro.dismiss();
}
}
};
}
}
在 application类 或 welcome类 或 Login类 中,初始化请求队列
RequestManager.init(this);
5.volley 在Activity中使用
public class LoginActivity extends BaseActivity {
private void loadDataByPost(){
JSONObject json=new JSONObject();
try {
jsondata.put("U_Name", “zs”);
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest request =new JsonObjectRequest(URL, json, new Listener(){
@Override
public void onResponse(JSONObject response) {
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
executeRequest(request);
}
}
private void getDataByGet(){
StringRequest mStringRequest = new StringRequest(Request.Method.GET, "http://www.baidu.com",
new Response.Listener() {
@Override
public void onResponse(String response) {
Log.i("wangshu", response);+
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("wangshu", error.getMessage(), error);
}
});
executeRequest(mStringRequest );
}