0
我为我们的全球Leadbord编写了一个服务器,现在它实际上可以工作。 如果它处于活动状态,我可以向其发送数据。但是,如果它失败了,我的应用程序不会停止。我没有得到它的解决方案,我希望你能帮助我。这里是发送:如果服务器宕机,TCPsending会崩溃应用程序
public void saveToDatabase(LeadboardElement element2) {
final LeadboardElement element = element2;
send = false;
// Need to be a thread! else android blocks it because it could take to
// long to send!
this.thread = new Thread() {
public void run() {
try {
Socket soc = new Socket(Config.TCP_SERVERNAME_IP,
Config.TCP_PORT);
DataOutputStream out = new DataOutputStream(
soc.getOutputStream());
DataInputStream in = new DataInputStream(
new BufferedInputStream(soc.getInputStream()));
// to call the save statement!
out.writeInt(0);
// give the stuff
out.writeUTF(element.getName());
out.writeInt(element.getLevel());
out.writeInt(element.getKillPoints());
// close it
out.close();
in.close();
soc.close();
send = true;
//join at every error
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
};
// start it
thread.start();
// join thread
if (!send) {
boolean retry = true;
while(retry)
try {
this.thread.join();
retry = false;
Log.w(TAG, "sending to server stopped!");
} catch (InterruptedException e2) {
Log.w(TAG, "Thread could not be joined");
}
}
}
我注意到,我需要做的是在一个线程,因为API 5所以它是这样工作的。如果玩家触摸屏幕,则在游戏结束时调用它。一切都停止了,数据被发送到服务器。如果他们失败了,我们会陷入黑色的褪色。 我想我需要像超时。我用CountDownTimer
试了一下,但这个实际上并没有解决问题。
非常感谢!
我很遗憾的转播,我只是没有找到这个解决方案。非常感谢快速帮助它修复它! – BennX