我想每秒检查一下我的MySQL数据库并从中获取数据
这是我尝试过的,我在一个单独的线程中建立了连接并让它睡眠
主题:
class Updater extends Thread {
ArrayList<chatData> list = new ArrayList<chatData>();
Boolean thereIsNewData = false;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String yourID = prefs.getString("KEY_USERNAME", "No login info available");
void getData(){
chatServiceHandler jsonParser = new chatServiceHandler();
ArrayList<NameValuePair> nvp = new ArrayList<NameValuePair>();
nvp.add(new BasicNameValuePair("yourID", yourID));
String json = jsonParser.makeServiceCall("http://example.com/getMSG.php", contactsServiceHandler.GET, nvp);
Log.d("Response: ", "> " + json);
if (json != null) {
try {
JSONObject jsonObj = new JSONObject(json);
if (jsonObj != null) {
list.clear();
JSONArray contactsArray = jsonObj.getJSONArray("MSGhandling");
for (int i = 0; i < contactsArray.length(); i++) {
JSONObject catObj = (JSONObject) contactsArray.get(i);
chatData cat = new chatData(catObj.getString("sender"), catObj.getString("receiver"),catObj.getString("msg"),catObj.getString("msgID"));
list.add(cat);
thereIsNewData = true;
}
}
} catch (JSONException e) {
e.printStackTrace();
thereIsNewData = false;
}
} else {
Log.e("JSON Data", "Didn't receive any data from server!");
thereIsNewData = false;
}
}
@Override
public void run() {
while (true) {
try {
DatabaseAdapter helper = new DatabaseAdapter(UpdaterService.this);
getData();
if(thereIsNewData){
for(int i =0; i < list.size(); i++){
chatData messages = list.get(i);
Log.d(TAG, "From: "+messages.getSenderID()+" TO:"+messages.getReceiverID()+" MSG: "+messages.getMSG());
helper.storeMSG(messages.getSenderID() , messages.getReceiverID() , messages.getMSG() , messages.getMsgID());
Intent newMessage = new Intent();
newMessage.setAction("NEW_MESSAGE");
sendBroadcast(newMessage);
}
list.clear();
thereIsNewData = false;
}else{
Log.d(TAG, "There is no new data");
}
sleep(1000);
} catch (InterruptedException e) {
}
}
}
}
它确实有效,但在几秒钟后停止半分钟并显示错误而不是回来再做同样的事情:
-----------------------------------的修改 ---- ----------------------------
LOG_CAT:
`190-318/system_process E/ThrottleService﹕ problem during onPollAlarm: java.lang.IllegalStateException: problem parsing stats: java.io.FileNotFoundException: /proc/net/xt_qtaguid/iface_stat_all: open failed: ENOENT (No such file or directory) 03
04-02 10:37:44.162 389-403/com.android.systemui E/JavaBinder﹕ *** Uncaught remote exception! (Exceptions are not yet supported across processes.)
java.lang.RuntimeException: android.os.DeadObjectException
at android.os.Parcel.writeException(Parcel.java:1326)
at android.os.Binder.execTransact(Binder.java:370)
at dalvik.system.NativeStart.run(Native Method)
Caused by: android.os.DeadObjectException
at android.os.BinderProxy.transact(Native Method)
at android.content.IIntentReceiver$Stub$Proxy.performReceive(IIntentReceiver.java:121) at android.app.ActivityThread$ApplicationThread.scheduleRegisteredReceiver(ActivityThread.java:771) at android.app.ApplicationThreadNative.onTransact(ApplicationThreadNative.java:381)
at android.os.Binder.execTransact(Binder.java:367)
at dalvik.system.NativeStart.run(Native Method)
04-02 10:41:00.282 630-644/com.khaled.zg W/System.err﹕ org.apache.http.conn.HttpHostConnectException: Connection to http://example.com refused
04-02 10:41:00.292 630-644/com.khaled.zg W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
04-02 10:41:00.292 630-644/com.khaled.zg W/System.err﹕ at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-02 10:41:00.292 630-644/com.khaled.zg W/System.err﹕ at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-02 10:41:00.306 630-644/com.khaled.zg W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
04-02 10:41:00.306 630-644/com.khaled.zg W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-02 10:41:00.306 630-644/com.khaled.zg W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-02 10:41:00.306 630-644/com.khaled.zg W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-02 10:41:00.313 630-644/com.khaled.zg W/System.err﹕ at com.khaled.zg.contactsServiceHandler.makeServiceCall(contactsServiceHandler.java:79)
04-02 10:41:00.326 630-644/com.khaled.zg W/System.err﹕ at com.khaled.zg.UpdaterService$Updater.getContacts(UpdaterService.java:101)
04-02 10:41:00.326 630-644/com.khaled.zg W/System.err﹕ at com.khaled.zg.UpdaterService$Updater.run(UpdaterService.java:135)
04-02 10:41:00.333 630-644/com.khaled.zg W/System.err﹕ Caused by: java.net.ConnectException: failed to connect to /185.27.134.163 (port 80): connect failed: ETIMEDOUT (Connection timed out)
04-02 10:41:00.345 630-644/com.khaled.zg W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:114)
04-02 10:41:00.345 630-644/com.khaled.zg W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
04-02 10:41:00.345 630-644/com.khaled.zg W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
04-02 10:41:00.353 630-644/com.khaled.zg W/System.err﹕ at java.net.Socket.connect(Socket.java:842)
04-02 10:41:00.353 630-644/com.khaled.zg W/System.err﹕ at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
04-02 10:41:00.364 630-644/com.khaled.zg W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
04-02 10:41:00.364 630-644/com.khaled.zg W/System.err﹕ ... 9 more
04-02 10:41:00.364 630-644/com.khaled.zg W/System.err﹕ Caused by: libcore.io.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
04-02 10:41:00.383 630-644/com.khaled.zg W/System.err﹕ at libcore.io.Posix.connect(Native Method)
04-02 10:41:00.383 630-644/com.khaled.zg W/System.err﹕ at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
04-02 10:41:00.393 630-644/com.khaled.zg W/System.err﹕ at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
04-02 10:41:00.393 630-644/com.khaled.zg W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:112)`
我做错了吗?我的问题还有其他办法或解决办法吗?
主机是导致问题的原因吗?