简体   繁体   中英

Android Application crashes in while loop

I have an android app connected with my database using volley/JSON.I want to check continuously the table "request" of a user in my database and when it has a value equal to 1 I want an alert message.I used while loop to handle it but the application crashes.Any ideas how to avoid using while? Here is my java code I run into the mainactivity in onCreate method:

     while(request!=1)  {

        Response.Listener<String> response1Listener = new Response.Listener<String>() {
            @Override
            public void onResponse(final String response) {


                try {
                    JSONObject jsonResponse = new JSONObject(response);


                    request= jsonResponse.getInt("request");
                    requestorigin=jsonResponse.getString("requestorigin");

                     //Toast.makeText(MainActivity.this,"Request from    " + requestorigin ,Toast.LENGTH_LONG).show();
                    // Toast.makeText(MainActivity.this,"Request from    " + request ,Toast.LENGTH_LONG).show();

                    if(request==1)  {
                        Toast.makeText(MainActivity.this,"Request from    " + requestorigin ,Toast.LENGTH_LONG).show();

                    }
                } catch (
                        JSONException e
                        )
                {
                    e.printStackTrace();
                }
            }
        };

        CheckRequest checkRequest = new CheckRequest(username,response1Listener);
        RequestQueue queue1 = Volley.newRequestQueue(MainActivity.this);
        queue1.add(checkRequest);
    }

And here is the CheckRequest class:

package com.example.sakis.multiplayerdemo;

import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;

import java.util.HashMap;
import java.util.Map;

public class CheckRequest extends StringRequest {
    private static final String REGISTER_REQUEST_URL = "http://xxxxx.com/CheckRequest.php";
private Map<String, String> params;

public CheckRequest(String username, Response.Listener<String> listener) {
    super(Method.POST, REGISTER_REQUEST_URL, listener, null);
    params = new HashMap<>();
    params.put("username", username  );
}

@Override
public Map<String, String> getParams() {
    return params;
}
}

The logcat shows this:

29582-29582/com.example.sakis.multiplayerdemo D/dalvikvm: VFY: replacing 
opcode 0x6f at 0x120b 05-21 12:38:17.236 29582-    
29582/com.example.sakis.multiplayerdemo E/dalvikvm: Could not find class 
'android.os.PersistableBundle', referenced from method 
com.example.sakis.multiplayerdemo.MainActivity.access$super 05-21 
12:38:17.236 29582-29582/com.example.sakis.multiplayerdemo W/dalvikvm: VFY: 
unable to resolve check-cast 222 (Landroid/os/PersistableBundle;) in 
Lcom/example/sakis/multiplayerdemo/MainActivity; 

The problem is the while loop is spinning waiting for request to change, which only happens in the response listener. The listener is called asynchronously, so you are effectively blocking your main thread and repeatedly queueing the same request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM