早上好,我正在制作一个游戏,我正在努力保持MYSQL数据库中的分数,并遇到了这个我无法解决的错误,如果有人给我podesse感激的小帮助。 (抱歉英语不好)
UserFunctions.java
public JSONObject savegame(int fuid, int fscore, int fenergy, int ftime){
// Building Parameters
String uid= Integer.toString(fuid);
String score= Integer.toString(fscore);
String energy= Integer.toString(fenergy);
String time= Integer.toString(ftime);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", save_tag));
params.add(new BasicNameValuePair("uid", uid));
params.add(new BasicNameValuePair("score", score));
params.add(new BasicNameValuePair("energy", energy));
params.add(new BasicNameValuePair("time", time));
JSONObject json = jsonParser.getJSONFromUrl(URL,params);
return json;
}
Game.java(游戏结束)
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.savegame(22, 100, 3, 60);
System.exit(0);
的index.php
else if ($tag == 'savescore') {
$uid = $_POST['uid'];
$score = $_POST['score'];
$energy = $_POST['energy'];
$time = $_POST['time'];
$result = $db->savescore($uid, $score, $energy, $time);
}
DB_Functions.php
public function savescore($uid, $score, $energy, $time) {
$result = mysql_query("INSERT INTO tk_users_info(uid, score, energy, time) VALUES('$uid', '$score', '$energy', '$time')");
// check for successful store
if ($result) {
// get user details
$uid = mysql_insert_id(); // last inserted id
$result = mysql_query("SELECT * FROM tk_users_info WHERE uid = $uid");
// return user details
return mysql_fetch_array($result);
} else {
return false;
}
}
错误讯息:
07-15 10:15:54.467: E/InputEventReceiver(3745): Exception dispatching input event.
07-15 10:15:54.467: E/MessageQueue-JNI(3745): Exception in MessageQueue callback: handleReceiveCallback
07-15 10:15:54.477: E/MessageQueue-JNI(3745): android.os.NetworkOnMainThreadException
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at java.net.InetAddress.getAllByName(InetAddress.java:214)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at com.terrakius.smashrain.library.JSONParser.getJSONFromUrl(JSONParser.java:42)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at com.terrakius.smashrain.library.UserFunctions.savegame(UserFunctions.java:100)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at com.terrakius.smashrain.game.RainView.onTouchEvent(RainView.java:161)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at android.view.View.dispatchTouchEvent(View.java:7706)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at android.view.View.dispatchPointerEvent(View.java:7886)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3947)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3826)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3442)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3411)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3518)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3419)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3575)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3442)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3411)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3419)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5532)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5512)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5483)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5612)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at android.os.MessageQueue.nativePollOnce(Native Method)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at android.os.MessageQueue.next(MessageQueue.java:138)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at android.os.Looper.loop(Looper.java:123)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at android.app.ActivityThread.main(ActivityThread.java:5001)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at java.lang.reflect.Method.invokeNative(Native Method)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at java.lang.reflect.Method.invoke(Method.java:515)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-15 10:15:54.477: E/MessageQueue-JNI(3745): at dalvik.system.NativeStart.main(Native Method)
07-15 10:15:54.477: D/AndroidRuntime(3745): Shutting down VM
07-15 10:15:54.477: W/dalvikvm(3745): threadid=1: thread exiting with uncaught exception (group=0x415deba8)
07-15 10:15:54.477: E/AndroidRuntime(3745): FATAL EXCEPTION: main
07-15 10:15:54.477: E/AndroidRuntime(3745): Process: com.terrakius.smashrain, PID: 3745
07-15 10:15:54.477: E/AndroidRuntime(3745): android.os.NetworkOnMainThreadException
07-15 10:15:54.477: E/AndroidRuntime(3745): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
07-15 10:15:54.477: E/AndroidRuntime(3745): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
07-15 10:15:54.477: E/AndroidRuntime(3745): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
07-15 10:15:54.477: E/AndroidRuntime(3745): at java.net.InetAddress.getAllByName(InetAddress.java:214)
07-15 10:15:54.477: E/AndroidRuntime(3745): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
07-15 10:15:54.477: E/AndroidRuntime(3745): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-15 10:15:54.477: E/AndroidRuntime(3745): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-15 10:15:54.477: E/AndroidRuntime(3745): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
07-15 10:15:54.477: E/AndroidRuntime(3745): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-15 10:15:54.477: E/AndroidRuntime(3745): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-15 10:15:54.477: E/AndroidRuntime(3745): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-15 10:15:54.477: E/AndroidRuntime(3745): at com.terrakius.smashrain.library.JSONParser.getJSONFromUrl(JSONParser.java:42)
07-15 10:15:54.477: E/AndroidRuntime(3745): at com.terrakius.smashrain.library.UserFunctions.savegame(UserFunctions.java:100)
07-15 10:15:54.477: E/AndroidRuntime(3745): at com.terrakius.smashrain.game.RainView.onTouchEvent(RainView.java:161)
07-15 10:15:54.477: E/AndroidRuntime(3745): at android.view.View.dispatchTouchEvent(View.java:7706)
07-15 10:15:54.477: E/AndroidRuntime(3745): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
07-15 10:15:54.477: E/AndroidRuntime(3745): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
07-15 10:15:54.477: E/AndroidRuntime(3745): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
07-15 10:15:54.477: E/AndroidRuntime(3745): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
07-15 10:15:54.477: E/AndroidRuntime(3745): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
07-15 10:15:54.477: E/AndroidRuntime(3745): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
07-15 10:15:54.477: E/AndroidRuntime(3745): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)
07-15 10:15:54.477: E/AndroidRuntime(3745): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
07-15 10:15:54.477: E/AndroidRuntime(3745): at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
07-15 10:15:54.477: E/AndroidRuntime(3745): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
07-15 10:15:54.477: E/AndroidRuntime(3745): at android.view.View.dispatchPointerEvent(View.java:7886)
07-15 10:15:54.477: E/AndroidRuntime(3745): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3947)
07-15 10:15:54.477: E/AndroidRuntime(3745): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3826)
07-15 10:15:54.477: E/AndroidRuntime(3745): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
07-15 10:15:54.477: E/AndroidRuntime(3745): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3442)
07-15 10:15:54.477: E/AndroidRuntime(3745): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3411)
07-15 10:15:54.477: E/AndroidRuntime(3745): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3518)
07-15 10:15:54.477: E/AndroidRuntime(3745): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3419)
07-15 10:15:54.477: E/AndroidRuntime(3745): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3575)
07-15 10:15:54.477: E/AndroidRuntime(3745): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
07-15 10:15:54.477: E/AndroidRuntime(3745): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3442)
07-15 10:15:54.477: E/AndroidRuntime(3745): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3411)
07-15 10:15:54.477: E/AndroidRuntime(3745): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3419)
07-15 10:15:54.477: E/AndroidRuntime(3745): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
07-15 10:15:54.477: E/AndroidRuntime(3745): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5532)
07-15 10:15:54.477: E/AndroidRuntime(3745): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5512)
07-15 10:15:54.477: E/AndroidRuntime(3745): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5483)
07-15 10:15:54.477: E/AndroidRuntime(3745): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5612)
07-15 10:15:54.477: E/AndroidRuntime(3745): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
07-15 10:15:54.477: E/AndroidRuntime(3745): at android.os.MessageQueue.nativePollOnce(Native Method)
07-15 10:15:54.477: E/AndroidRuntime(3745): at android.os.MessageQueue.next(MessageQueue.java:138)
07-15 10:15:54.477: E/AndroidRuntime(3745): at android.os.Looper.loop(Looper.java:123)
07-15 10:15:54.477: E/AndroidRuntime(3745): at android.app.ActivityThread.main(ActivityThread.java:5001)
07-15 10:15:54.477: E/AndroidRuntime(3745): at java.lang.reflect.Method.invokeNative(Native Method)
07-15 10:15:54.477: E/AndroidRuntime(3745): at java.lang.reflect.Method.invoke(Method.java:515)
07-15 10:15:54.477: E/AndroidRuntime(3745): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-15 10:15:54.477: E/AndroidRuntime(3745): at com.android.internal.os.ZygoteInit.main(ZygoteInit.
如果需要更多代码来帮助我。谢谢。
答案 0 :(得分:1)
您正在使用UI线程来消耗HTTP-Request。这将使您的应用程序没有响应。 Android正在杀死你的应用程序,然后它可以做任何类似的事情。
看一下你需要的AsyncTask
。