我正在尝试使用带有okHttp的Google Volley。我按照本教程http://www.androidhive.info/2014/05/android-working-with-volley-library-1/进行所有设置。我设置了我的凌空单例和LruBitmapCache。使用他们的get字符串请求,但是每次我发出请求时,都会得到NullPointerException。
7717-8280/com.admin.zipline E/AndroidRuntime﹕ FATAL EXCEPTION: pool-9-thread-1
Process: com.admin.zipline, PID: 7717
java.lang.NullPointerException
at com.admin.zipline.activities.AccountVerification.getTransactionsDetails(AccountVerification.java:295)
at com.admin.zipline.activities.AccountVerification_.access$701(AccountVerification_.java:25)
at com.admin.zipline.activities.AccountVerification_$8.execute(AccountVerification_.java:203)
at org.androidannotations.api.BackgroundExecutor$Task.run(BackgroundExecutor.java:302)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
这是我的要求
错误在第295行上,为
VolleyNetwork.getInstance().addToRequestQueue(strReq, tag_string_req);
@Background
public void getTransactionsDetails() {
String url = NetworkConstants.Url_transactions;
// Tag used to cancel the request
String tag_string_req = "string_req";
Log.i("URL",url);
StringRequest strReq = new StringRequest(Request.Method.GET,
url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("Response", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("Error", "Error: " + error.getMessage());
}
}){
/**
** Passing some request headers
* */
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
headers.put("Authorization", VolleyNetwork.authorization);
return headers;
}
};
// Adding request to request queue
if(VolleyNetwork.getInstance() == null){
Log.i("NULL", "NETWORK NULL");
}else {
VolleyNetwork.getInstance().addToRequestQueue(strReq, tag_string_req);
}
}
除了在顶部添加一些全局变量外,我的单例设置几乎完全相同。
VolleyNetwork.java
public class VolleyNetwork extends Application {
Context context;
public static String access_token,token_type,user_id,name, authorization;
public VolleyNetwork(Context context) {
this.context = context;
SharedPreferences preferences = context.getSharedPreferences(FileNames.login_details, Context.MODE_PRIVATE);
access_token = preferences.getString(NetworkConstants.acess_token, "");
token_type = preferences.getString(NetworkConstants.token_type, "");
user_id = preferences.getString(NetworkConstants.user_id, "");
authorization = token_type + " " + access_token;
}
public static final String TAG = VolleyNetwork.class
.getSimpleName();
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static VolleyNetwork mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized VolleyNetwork getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public ImageLoader getImageLoader() {
getRequestQueue();
if (mImageLoader == null) {
mImageLoader = new ImageLoader(this.mRequestQueue,
new LruBitmapCache());
}
return this.mImageLoader;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
// set the default tag if tag is empty
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
我认为这可能与我的清单有关。我不太确定该如何设置。我之前的人设置了大部分应用程序,而我对Java并不那么熟悉。
现在,清单使用
android.permission.INTERNET permission
,应用程序的名称为.activities.MyApplication
。 MyApplication扩展了Application。我试图将应用程序名称设置为.network.VolleyNetwork
,并让VolleyNetwork扩展MyApplication,但是得到了“ VolleyNetwork没有默认构造函数”。不太确定该怎么做。
最佳答案
发生这种情况是因为您实际上并不是在创建对象VolleyNetwork。
单例模式的工作方式如下:
public class MySingleton {
private static MySingleton instance;
private MySingleton(){
//constructor here
}
public static MySingleton getInstance(){
if(instance==null){
instance = new MySingleton();
}
return instance;
}
}
请注意,静态
getInstance
方法实际上会创建您的实例,并确保它保持存储在static字段中,而在您的情况下,它将检索默认值null。另外,请确保将构造函数设为私有,以便只能通过公共
getInstance
方法检索实例,否则任何人都可以调用构造函数并创建尽可能多的实例。