在网络服务响应的基础上,我决定开放活动。
SELECT split_part(data, '-----BEGIN CERTIFICATE REQUEST-----', 2)
FROM openca2.request
WHERE req_key = 3874;
getCompanyByUser()是我调用webservice的方法。
if(getCompanyByUser().size() > 0) {
Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(intent);
finish();
} else {
Intent intent = new Intent(SplashScreenActivity.this, SelectCompany.class);
startActivity(intent);
finish();
}
现在在此列表中始终为null,因为它在后台填充。所以现在它将始终打开SelectCompany活动。
由于private ArrayList<CompApp> getCompanyByUser() {
final ArrayList<CompApp> list = new ArrayList<CompApp>();
showpDialog(pDialog);
StringRequest stringRequest = new StringRequest(Request.Method.POST, getCompByUser,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("response",response);
try{
CompApp[] access = new Gson().fromJson(response, CompApp[].class);
for (CompApp c : access) {
list.add(c);
}
helper.addCompany(list);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
String dateString = formatter.format(new java.util.Date());
saveSharedPref(SplashScreenActivity.this, "timestamp", dateString);
}catch (Exception e) {
Toast.makeText(SplashScreenActivity.this, "Unable to process request.\nTry again.", Toast.LENGTH_SHORT).show();
}
hidepDialog(pDialog);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(SplashScreenActivity.this, "Internet not connected.", Toast.LENGTH_LONG).show();
hidepDialog(pDialog);
}
}) {
@Override
protected Map<String,String> getParams() {
Map<String,String> params = new HashMap<String, String>();
User user = new Gson().fromJson(getSharePref(SplashScreenActivity.this, "user"), User.class);
String lastUpdated = getSharePref(SplashScreenActivity.this, "lastUpdated");
params.put("appCode","PCA001");
params.put("userId",user.id);
params.put("timestamp",""+lastUpdated);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(SplashScreenActivity.this);
requestQueue.add(stringRequest);
return list;
}
,我的应用程序崩溃了两个问题,我希望程序应该等到列表填满。
以下是java.lang.IllegalArgumentException
logcat
答案 0 :(得分:2)
此处的代码流程不正确。当您调用getCompanyByUser()时,它是异步调用。因此,您需要在继续之前等待其响应。
所以只需致电getCompanyByUser()
即可。将其返回类型更改为void,因为它是异步调用,因此不需要。现在,您应该在onResponse和ErrorResponse回调中开始您的活动。
private void getCompanyByUser() {
//....
@Override
public void onResponse(String response) {
// ...
//Once the list is made start new activity
if(list.size() > 0) {
Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(intent);
finish();
} else {
Intent intent = new Intent(SplashScreenActivity.this, SelectCompany.class);
startActivity(intent);
finish();
}
}
}
答案 1 :(得分:0)
你只有退货才有问题。您将在onResponse()
填充列表之前返回,因为onResponse
是异步的,因此您不必等待它并返回一个空列表。第二,异常是因为你正在做一些对话事件(你将持有你的活动的引用)以响应你的onResponse()
方法,但是当前的活动已经完成,因此引发了异常。
最佳解决方案是使用Handler,并提供成功和失败的信息。在您的响应/失败中发送回调给处理程序,并在您的处理程序handleMessage(Message msg)
中决定应该从哪个活动开始。
这样的事情: 在你的活动中制作一个像这样的处理程序
Handler handler =new Handler() {
@Override
public void handleMessage(Message msg) {
// you may check success or failure on msg based.
// handle your next activity check here
}
};
并在onResponse()
或onFailure()
向处理程序发送消息,例如
handler.sendEmptyMessage()
或handler.sendMessage(msg)