简体   繁体   English

Android应用程序在while循环中崩溃

[英]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? 我有一个使用volley / JSON与我的数据库连接的android应用程序。我想连续检查数据库中用户的表“ request”,当它的值等于1时,我希望收到一条警报消息。处理它,但应用程序崩溃。任何想法如何避免使用while? Here is my java code I run into the mainactivity in onCreate method: 这是我在onCreate方法中遇到mainactivity的Java代码:

     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: 这是CheckRequest类:

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. 问题是while循环正在旋转,等待更改request ,这仅在响应侦听器中发生。 The listener is called asynchronously, so you are effectively blocking your main thread and repeatedly queueing the same request. 侦听器被异步调用,因此您有效地阻塞了主线程并重复排队相同的请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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