我有一个使用volley / JSON连接我的数据库的Android应用程序。我想连续检查我的数据库中用户的表“请求”,当它的值等于1时,我想要一条警报消息。我用过虽然循环处理它但应用程序崩溃。任何想法如何避免使用时? 这是我在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);
}
这是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;
}
}
logcat显示:
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;
答案 0 :(得分:0)
问题是while循环正在旋转等待request
更改,这只发生在响应侦听器中。侦听器是异步调用的,因此您实际上会阻塞主线程并反复排队相同的请求。