首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP的Java - URLConnection用于MySQL的$_GET方法

PHP的Java - URLConnection用于MySQL的$_GET方法
EN

Stack Overflow用户
提问于 2010-09-02 07:41:06
回答 2查看 1.2K关注 0票数 0

好了,这有什么问题呢?我尝试使用PHP的JConnector,但是人们说不要,因为它可以包含你的小程序的细节,所以我告诉他们我会使用MySQL的$_GET方法URL。他们说会很好的。然而,我发现了两个问题。

1.)他们很慢。URLConnection至少需要4-5秒才能发生。即使我将它指向localhost。

2.)这是大量的代码。也许我只是做错了?

这就是我所拥有的--它是有效的!

代码语言:javascript
复制
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();
        }
    }
}

下面是我们的回应:

代码语言:javascript
复制
Let's change the level!
You're Lvl is: 123456
Trying to update level...
You're NEW Lvl is: 777
Level update done!

它可以工作,但是速度慢而且笨重--我该如何解决这个问题呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-09-02 07:47:22

我会确保你的MySQL数据库有正确的索引设置。没有正确的MySQL indexes设置会让你的代码慢很多!

但是,如果是您的PHP代码导致了问题,那么您应该将其作为与Java端相反的代码发布。由于PHP代码处理的是数据库,我打赌这就是问题所在。

票数 1
EN

Stack Overflow用户

发布于 2010-09-02 12:51:28

问题是URLConnection实际上相当慢。您可以尝试将Proxy.NO_PROXY参数传递给openConnection,但这不会让它变得非常快。(每次调用1到3秒,所以您仍然会占用大量时间) HttpConnection的替代方案可能是Jakarta Commons HttpClient

实际上很有趣的是,更新你关卡的网址是开着的:如果有人发现了这一点,你的游戏将会有一大堆作弊的人在使用你的游戏。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3622879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档