您的位置:首页 > 其它

Volley使用略记

2016-05-25 15:54 369 查看
VolleyDao的使用略记
知识点:
1、volley的单例模式建立;
2、建立volley的json和图片请求接口;
3、volley调用例子;
4、volley使用的错误解决;

我们要建立一个volley的类,创建请求json 和图片的接口,用于给外部调用。代码如下
<span style="font-size:14px;">public class  VolleyDao {
private static RequestQueue queue;
private static Context mContext;
private static VolleyDao instance = null;

//单例模式
public static VolleyDao getInstance(Context context){
if(instance==null){
instance = new VolleyDao();
mContext=context;
queue = Volley.newRequestQueue(context);// 获取一个请求队列对象
}
return instance ;
}

//私有化构造函数
private VolleyDao(){

}

/**
* 发JSONObject类型的请求过去,回来JSONObject
* @param request
* @param responListener
* @param errorListener
*/
public void postToService(JSONObject request,Listener<JSONObject> responListener,ErrorListener errorListener) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Method.POST, Constants.SERVER_URL, request, responListener,
errorListener);
queue.add(jsonObjectRequest);// 添加请求到队列里
}

/**
* 发个String类型的图片地址过去,回来Bitmap
* @param imageUrl
* @param responListener
* @param errorListener
*/
public void postToService(String imageUrl,Listener<Bitmap> responListener,ErrorListener errorListener) {
ImageRequest imageRequest = new ImageRequest(imageUrl,
responListener, 0, 0, Config.ARGB_8888, errorListener);
imageRequest.shouldCache();// 缓存文件在/data/data/包名/cache/volley,如果已经存在则不再去下载了。
queue.add(imageRequest);

}

}
</span>


可以看到上面volley类创建的两个方法,传入的参数我们可以依次创建传入。
对于json对象我们可以自己创建一个,创建一个登陆的网络请求。例如:

<span style="font-size:14px;">// Login
public JSONObject newLoginRequest(String username, String password) {
request = new JSONObject();
try {
request.put("timestamp", System.currentTimeMillis() / 1000);
request.put("password", password);
request.put("username", username);
//还可以添加自己需要的参数
} catch (JSONException e) {
e.printStackTrace();
}
return request;
}
</span>


返回成功监听
<span style="font-size:14px;">Listener<JSONObject> responListener = new Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//成功登陆后续操作
}
};</span>


返回错误监听
<span style="font-size:14px;">errorListener = new ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//失败后续操作
}
};</span>


最后我们只要实现回调监听器,并且传入就OK了。
<span style="font-size:14px;">JSONObject request = volleyRequest.newLoginRequest(account, psw);
volleyDao.postToService(request, responListener, errorListener);</span>


我们只需要操作的是,在返回是否成功的回调监听上面进行相应的操作。
是不是很简单,也很实用,我们只需要关心传参和回调监听即可。对于底层的如何访问网络我们就不用担心了。volley已经帮我们实现了。

友情提示:

但是我在一个项目中遇到一个找回密码,重复重置了两次密码,而用户则只收到第一条短信密码的通知。所以导致了用户明明修改了得到了短信密码,但是又提示密码不正确。
我找了很久,发现程序确实发出了两条找回密码的网络请求,间隔时间在3秒左右,但是发现我的代码里面压根没有发送两条请求的操作。最后只有去找volley的源码,终于发现一个问题,就是volley默认在2.5秒时间内没有收到请求,就会重新发送一次请求。(默认的重试次数为2次)。原因就在这里。
因为数据后台是这样操作的:先收到找回密码的请求,然后根据发过来的电话号码,去请求云端给这个号码发送一条重置的短信密码给用户,而设置发送短信是一小时之内3次。因为涉及到两个平台的访问,所以时间很大可能会超出2.5秒。所以就回出现以上的情况。
我在这里把他的timeout时间改成30秒。然后系统就正常了。

注意:这里我们提醒,我们还需要观看volley重复请求的最大次数,timeout等等常量数据,可以到源码里面进行相应的修改。

如有任何问题,请及时与我联系,谢谢。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: