由于请求缓慢,我的应用程序崩溃了。 我请求一个名为“结果”的json登录。
myRequest.AuthenticationRequest(username,password,getApplicationContext());
final Handler handlerC = new Handler();
handlerC.postDelayed(new Runnable() {
@Override
public void run() {
String result = myRequest.getResult();
if(result.equals("success"))
{
userInfoHolder.setSESSION(username);
userInfoHolder.RequestUserInfo(getApplicationContext());
userInfoHolder.RequestUserOtherInfo(getApplication());
final Handler handlerC = new Handler();
handlerC.postDelayed(new Runnable() {
@Override
public void run() {
finish();
Intent intent = new Intent(Home.this,User.class);
startActivity(intent);
}
}, 500);
}
else
{
Toast.makeText(Home.this, ""+result, Toast.LENGTH_SHORT).show();
}
}
}, 500);
public void AuthenticationRequest(final String username, final String password,Context c)
{
requestQueue = Volley.newRequestQueue(c);
request = new StringRequest(Request.Method.POST, controllerLinks.getUrlLogin(), new Response.Listener<String>()
{
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
setResult(jsonObject.getString("result"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {@Override public void onErrorResponse(VolleyError error) {}}){ @Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String,String> hashMap = new HashMap<String, String>(){
{
put("login","clicked");
put("username",username);
put("password",password);
}
};
return hashMap;
}
};
requestQueue.add(request);
}
我试图通过使用postDelayed解决我的问题,但我认为有更好的方法来处理问题,我只是不知道该怎么做,请帮忙。
01-11 13:46:34.606 3697-3697/com.example.a1412.alzhaid E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.a1412.alzhaid, PID: 3697
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at com.example.a1412.alzhaid.Home$5.run(Home.java:252)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)