好了,这有什么问题呢?我尝试使用PHP的JConnector,但是人们说不要,因为它可以包含你的小程序的细节,所以我告诉他们我会使用MySQL的$_GET方法URL。他们说会很好的。然而,我发现了两个问题。
1.)他们很慢。URLConnection至少需要4-5秒才能发生。即使我将它指向localhost。
2.)这是大量的代码。也许我只是做错了?
这就是我所拥有的--它是有效的!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.Applet;
import java.awt.TextArea.*;
import java.util.*;
import java.net.*;
import java.applet.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class test extends JApplet
{
public JTextArea c;
public void init()
{
c = new JTextArea();
add(c);
c.append("Let's change the level!");
try
{
URL game = new URL("http://localhost/mystikrpg/game.php?act=stats&username=Dan");
URLConnection connection = game.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
{
String command = inputLine;
System.out.println(command);
String[] temp = command.split("\\|");
c.append("\nYou're Lvl is: " + temp[1]);
}
in.close();
c.append("\nTrying to update level...");
String newLevel = "777";
URL newGame = new URL("http://localhost/mystikrpg/game.php?act=updateLvl&username=Dan&lvl=" + newLevel);
URLConnection levelConnection = newGame.openConnection();
BufferedReader level_BR = new BufferedReader(new InputStreamReader(levelConnection.getInputStream()));
URL updateLevelURL = new URL("http://localhost/mystikrpg/game.php?act=stats&username=Dan");
URLConnection up_lvl_conn = updateLevelURL.openConnection();
BufferedReader up_lvl_br = new BufferedReader(new InputStreamReader(up_lvl_conn.getInputStream()));
String getLvl;
while ((getLvl = up_lvl_br.readLine()) != null)
{
String[] newLvl = getLvl.split("\\|");
c.append("\nYou're NEW Lvl is: " + newLvl[1]);
// newLvl[1] == newLevel
}
c.append("\nLevel update done!");
level_BR.close();
up_lvl_br.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}下面是我们的回应:
Let's change the level!
You're Lvl is: 123456
Trying to update level...
You're NEW Lvl is: 777
Level update done!它可以工作,但是速度慢而且笨重--我该如何解决这个问题呢?
发布于 2010-09-02 07:47:22
我会确保你的MySQL数据库有正确的索引设置。没有正确的MySQL indexes设置会让你的代码慢很多!
但是,如果是您的PHP代码导致了问题,那么您应该将其作为与Java端相反的代码发布。由于PHP代码处理的是数据库,我打赌这就是问题所在。
发布于 2010-09-02 12:51:28
问题是URLConnection实际上相当慢。您可以尝试将Proxy.NO_PROXY参数传递给openConnection,但这不会让它变得非常快。(每次调用1到3秒,所以您仍然会占用大量时间) HttpConnection的替代方案可能是Jakarta Commons HttpClient。
实际上很有趣的是,更新你关卡的网址是开着的:如果有人发现了这一点,你的游戏将会有一大堆作弊的人在使用你的游戏。
https://stackoverflow.com/questions/3622879
复制相似问题