这是我自己简单封装的volley,封装的不好哈,希望大家能多多提提意见,来让我改正!
1. 首先下载个Volley的jar包,拷贝到libs下,同步一下代码
2. 代码 ,注释挺详细的
创建一个Application,方便加入队列
package com.nsv.rolltv;
import android.app.Application;
import android.content.Context;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.nsv.rolltv.service.LocationService;
public class MyApplication extends Application {
private static RequestQueue queues;
public static Context mContext;
@Override
public void onCreate() {
super.onCreate();
mContext = this;
queues = Volley.newRequestQueue(getApplicationContext());
}
public static RequestQueue getHttpQueues() {
return queues;
}
}
在清单文件的application标签中加入
android:name=".MyApplication"
网络请求工具类
package com.nsv.rolltv.http;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.nsv.rolltv.MyApplication;
import com.nsv.rolltv.tools.Pref;
import com.nsv.rolltv.tools.ToastUtil;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
public class MyHttpRequest {
//private static ProgressDialog progressDialog;
/**
* volleyGet请求
*
* @param serverUrl 请求链接
* @param myHttpListener 成功的回调
*/
public static void sendGetRequest(final String serverUrl, Context context, HashMap<String, String> map, String tag, final MyHttpListener myHttpListener) {
/* final ProgressDialog progressDialog = new ProgressDialog(context);
progressDialog.setMessage("正在努力获取信息,请稍候......");
progressDialog.setTitle("努力争取中");
progressDialog.setCancelable(false);
progressDialog.show();*/
HashMap hashMap;
if (map == null) {
hashMap = new HashMap();
} else {
hashMap = map;
}
hashMap.put("userMark", unionid);
String url = serverUrl + getParams(hashMap);
Log.i("MyHttpRequest", "MyHttpRequest: " + url);
StringRequest getRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//progressDialog.cancel();
Log.i("MyHttpRequest", "返回信息:" + response);
if (TextUtils.isEmpty(response) || response == null) {
ToastUtil.showToast("抱歉,数据获取为空");
} else {
//这是一个接口,回调
myHttpListener.onSuccess(response);
}
}
},
//这是错误的回复,没进行变动
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//progressDialog.cancel();
error.printStackTrace();
MyApplication.ShareFlag=true;
ToastUtil.showToast("请求超时,请检查网络!");
}
}
);
//创建的MyAppLication继承AppLication,里面放一些全局变量
getRequest.setTag(tag);
//volley请求必须放到队列里
MyApplication.getHttpQueues().add(getRequest);
}
/**
* 发送post请求
*
* @param url
* @param hashMap
* @param tag
*/
public static void sendPostRequest(String url, Context context, HashMap hashMap, String tag, final MyPostHttpListener myPostHttpListener) {
/*final ProgressDialog progressDialog = new ProgressDialog(context);
progressDialog.setMessage("正在努力获取信息,请稍候......");
progressDialog.setTitle("努力争取中");
progressDialog.setCancelable(false);
progressDialog.show();*/
String unionid = Pref.getString("unionid", MyApplication.mContext);
if (hashMap == null) {
hashMap = new HashMap();
}
hashMap.put("userMark", unionid);
JSONObject jsonObject = new JSONObject(hashMap);
Log.i("HttpPostMsg", "sendPostRequest: " + url);
Log.i("HttpPostMsg", "sendPostCanshu: " + jsonObject.toString());
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, jsonObject,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {//jsonObject为请求返回的Json格式数据
//progressDialog.cancel();
Log.i("HttpPostMsg", "onResponse: " + jsonObject.toString());
myPostHttpListener.onSuccess(jsonObject);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
//progressDialog.cancel();
MyApplication.ShareFlag=true;
ToastUtil.showToast("网络连接超时,请检查网络。");
}
});
//设置请求的Tag标签,可以在全局请求队列中通过Tag标签进行请求的查找
request.setTag(tag);
//将请求加入全局队列中
MyApplication.getHttpQueues().add(request);
}
/**
* 获取参数
*
* @param map 请求的的参数
* @return
*/
public static String getParams(HashMap<String, String> map) {
//下面这两个参数是我们的每次请求必须带的,你可以忽略
//由于请求的后缀是这样的?jsonstr={"caID":"234234234234","userMark":"w2222"},
// 所以我只好这样拼接了
String s = "?jsonstr=";
JSONObject jsonObject = new JSONObject(map);
s = s + jsonObject.toString();
return s;
}
/**
* 接口HttpGet的回调
*/
public interface MyHttpListener {
public void onSuccess(String mresult);
}
/**
* 接口HttpPost的回调
*/
public interface MyPostHttpListener {
public void onSuccess(JSONObject jsonObject);
}
}