⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readrank.java

📁 经典的宇宙大战游戏代码(坦克大战)
💻 JAVA
字号:
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */
import java.io.*;
import javax.microedition.io.*;

public class ReadRank implements Runnable {
    private GalaxyWarCanvas sampleGame;
    String IP 	= "mgpp.mymobilesoft.com";
    String PORT 	= "80";
    String PATH 	= "/mgpp/RetrieveScore";

    // Please change the following parameters accordingly
    String gameid = "040414150053129";
    String recordNum = "10";
    String prefix 	= "http://" + IP + ":" + PORT + PATH;
    String requestBody = "gameid=" + gameid + "&recordnum=" + recordNum;
    String rankInfo = "";


    public ReadRank(GalaxyWarCanvas sampleGame) {
        this.sampleGame = sampleGame;
    }

    void start() {
        Thread t = new Thread(this);
        try {
            t.start();
        } catch (Exception e) {}
    }

    public void run() {
        try {
            rankInfo = requestScore(prefix, requestBody);
        } catch (IOException e) {
            rankInfo = "";
            System.out.println("readRank1:" + e);
        }
        try {
            rankInfo = formatRank(rankInfo);
        }catch(Exception e) {
            rankInfo = "";
        }
        sampleGame.rankInfo = rankInfo;
        sampleGame.isReaded = true;
        sampleGame.rankNum = sampleGame.getScoreNum(rankInfo);
    }

    private String requestScore(String urlString
        , String requestBodyString) throws IOException {
        HttpConnection c = null;
        InputStream is = null;
        OutputStream os = null;
        int rc;
        try {
            // Establish HTTP connection to game portal
            c = (HttpConnection) Connector.open(urlString, Connector.READ_WRITE);

            // Set the request method to POST
            c.setRequestMethod(HttpConnection.POST);
            c.setRequestProperty("User-Agent"
                , "Profile/MIDP-1.0 Configuration/CLDC-1.0");
            c.setRequestProperty("Content-Type"
                , "application/x-www-form-urlencoded");

            // Obtain output stream for sending the request string
            os = c.openOutputStream();
            os.write(requestBodyString.getBytes());

            // Getting the response code will open the connection,
            // send the request, and read the HTTP response headers.
            // The headers are stored until requested.

            rc = c.getResponseCode();
            if (rc != HttpConnection.HTTP_OK) {
                throw new IOException("HTTP response code: " + rc);
            }

            //Obtain input stream for receiving server response
            is = c.openDataInputStream();

            //Retrieve the response from the server
            int ch;
            StringBuffer b = new StringBuffer();

            // Get the Content-length header
            String lengthStr = c.getHeaderField("Content-length");
            int lengthInt = Integer.parseInt(lengthStr);

            // Get response data
            for (int i = 0; i < lengthInt; i++) {
                ch = is.read();
                b.append((char) ch);
            }
            return b.toString().toUpperCase();
        } catch (IOException e) {
            System.out.println("readRank2:" + e);
            return "";
        } finally {
            if (is != null) {
                is.close();
            }
            if (os != null) {
                os.close();
            }
            if (c != null) {
                c.close();
            }
        }
    }

    private String formatRank(String info) {
        int len = info.length();
        int index = 0;
        String currentRank = "",result = "";
        String score = "",user = "";
        boolean isBreak = false;
        if (len == 0) return "";
        for (int i=0;i<len;i++) {
            index = info.indexOf("RANK",4);
            if (index == -1) {
                currentRank = info;
                isBreak = true;
            }
            else {
                currentRank = info.substring(0, index);
                info = info.substring(index,info.length());
            }
            score = currentRank.substring(currentRank.indexOf("SCORE"));
            score = score.substring(6,score.indexOf(13)).trim();
            user = currentRank.substring(currentRank.indexOf("USER"));
            user = user.substring(5,user.indexOf(13)).trim();
            if (user.length() > 5)
                user = user.substring(0,5);
            result = result + score + " : " + user + ",";
            if (isBreak) break;
        }
        return result;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -