我实施了Volley singleton(互联网上的例子很少),以便从不同的活动中使用它。 由于某种原因,请求没有被发送到服务器,当我在调试模式下检查它时,它看起来在排队请求队列中有等待。
这是我的单身人士:
public class VolleyInternetOperator
{
private static VolleyInternetOperator mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mCtx;
private VolleyInternetOperator(Context context)
{
mCtx = context;
mRequestQueue = getRequestQueue();
mImageLoader = new ImageLoader(mRequestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(20);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
public static synchronized VolleyInternetOperator getInstance(Context context) {
if (mInstance == null) {
mInstance = new VolleyInternetOperator(context);
}
return mInstance;
}
public static synchronized VolleyInternetOperator getInstance()
{
if (null == mInstance)
{
throw new IllegalStateException(VolleyInternetOperator.class.getSimpleName() +
" is not initialized, call getInstance(...) first");
}
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
// getApplicationContext() is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {
getRequestQueue().add(req);
}
public ImageLoader getImageLoader() {
return mImageLoader;
}
public interface responseFunctionInterface
{
void apply(JSONObject response, JSONObject params);
}
public interface errorFunctionInterface
{
void apply(VolleyError error, JSONObject params);
}
public void accessWebsiteWithVolley(String url, int requestMethod,
final JSONObject params, final HashMap<String, String> additionalHeaders,
final responseFunctionInterface responseFunction, final errorFunctionInterface errorFunction)
{
CustomJSONObjectRequest jsonRequest =
new CustomJSONObjectRequest(requestMethod, url, params,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("got", "response");
responseFunction.apply(response, params);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("error", error.toString());
errorFunction.apply(error, params);
}
})
{
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
if (null != additionalHeaders) {
Iterator it = additionalHeaders.entrySet().iterator();
while (it.hasNext()) {
HashMap.Entry<String, String> pair = (Map.Entry<String, String>) it.next();
headers.put(pair.getKey(), pair.getValue());
}
}
return headers;
}
};
addToRequestQueue(jsonRequest);
}
}
我在主要活动onCreate上初始化了它:
VolleyInternetOperator.getInstance(this.getApplicationContext());
为了发送jsonRequest,我使用以下代码:
VolleyInternetOperator.getInstance().accessWebsiteWithVolley(url, requestMethod, null, headers,responseFunction, errorFunction);
当我将相关部分复制到我的活动时,请求有效,所以我猜这与单身人士有关。
谢谢!
编辑1:
显然,如果我从我的主要活动中调用单例,它是有效的,但如果我创建一个调用它的对象(我有dataCollector对象),它就不起作用。