我是Android的新手,我刚检查了排球安卓库,我想在我的自定义http请求中使用它,这在Android工作室的.getInstance
中失败了我试过以下
所以我正在使用android studio并拥有以下文件夹结构
com.geowan .....
frontend
1.LoginActivity
helpers
ApiSingleTon
所以在我的ApiSingleton中我有
public class ApiSingleton {
private static ApiSingleton mInstance;
private RequestQueue mRequestQueue;
private static Context mCtx;
private ImageLoader mImageLoader;
private ApiSingleton(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
....stuff copy pasted from volley docs on IMage disc cache loader
}
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;
}
}
现在我的java登录代码
ApiSingleton singleton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
login_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
trylogin(); //login method
}
});
}
tryLogin(){ //trying to emulate a http request
//proceed with authentication
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, loginurl, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i(TAG, response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO: Handle error
Log.i(TAG, error.toString());
}
});
}
singleton.getInstance(this).addToRequestQueue(jsonObjectRequest); //fails
上述内容在
处引发错误singleton.getInstance(this)
我哪里可能出错。
答案 0 :(得分:1)
首先我想你需要阅读更多关于Singleton的信息
第二
a[0]
这意味着您有一个被调用的函数getInstance返回类中的类类型
所以我想你需要添加这个
Integer.MIN_VALUE