Java - URLConnection for PHP $_GET Methods for MySQL
OK so what's the problem with this. I tried using MySQL JConnector for Java and people say not to because it can comprise your applet details so then I told them I'd use PHP $_GET method URLs using PHP scripts. They said it would be fine. However, I find two problems with that.
1.) They are slow. It takes at least 4-5 seconds for the URLConnection to happen. Even when I point it to localhost.
2.) It's a lot of code. Maybe I'm just doing it wrong?
Here is what I have -- which works!
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()) != nul开发者_运维问答l)
{
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();
}
}
}
and here is the response:
Let's change the level!
You're Lvl is: 123456
Trying to update level...
You're NEW Lvl is: 777
Level update done!
It works but it's slow and bulky - how do I fix this problem?
I would make sure your MySQL database has proper indexes setup. Not having proper MySQL indexes setup can slow your code down a ton!
But if it is your PHP code that is causing the issue, you should post that as appose to the Java side. Since the PHP Code deals with the database, my bet is that is where the hold up is.
The problem is that URLConnection is actually pretty slow. You could try passing the Proxy.NO_PROXY parameter to the openConnection, but it won't make it superfast. (1 to 3 seconds per call, so you're still using up a lot of time) An alternative to the HttpConnection could be the Jakarta Commons HttpClient.
It's actually also quite funny that the url's to update your level are left open: if someone finds out about that, you're going to have a whole lot of cheaters using your game.
精彩评论