j2me上传积分

/*
 * Created on 2007-3-14
 *
 * XXX To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
//package PS;

import javax.microedition.rms.*;
import java.io.*;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
/**
 * @author bluesky
 *
 * XXX To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class NetworkThread extends Thread {
   
    private static final String URL = "http://207.97.204.82:80/ranking.php";//"http://207.97.204.82:80/ranking.php"; //207.97.204.82 
    private HttpConnection httpConnection;
    String []mystring=null;
    public static int mycode;
    public static int mypoint;
    public String myname;
    String post_str;
    public static boolean sendOK;
    public static boolean urlok = true;
   
    public NetworkThread(int topSorce,String topName) {
        mystring=new String[12];
        mycode=0;
//        mypoint=67891;
//        myname="thomas";
        mypoint=topSorce;
        myname=topName;
        post_str=null;
        sendOK=false;//NetworkThread
       
    }

    private void connect(String str){
        try {
         httpConnection = (HttpConnection) Connector.open(URL);
      httpConnection.setRequestMethod(HttpConnection.POST);
         httpConnection.setRequestProperty("Content-Length",String.valueOf(str.length()));
         httpConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
        } catch (SecurityException ex) {
         urlok=false;
            System.out.println("you pressed no!");
//            ex.printStackTrace();
        }catch(Exception ex){
            urlok=false;
            System.out.println("you pressed connect no! is -->>> ");
            ex.printStackTrace();
        }
    }
   
    private void sendMessage(String str) {
        if(httpConnection == null || !urlok){
            System.out.println("you httpConnection is null!");
            return;
        }
        try {
            OutputStream out = httpConnection.openOutputStream();
            System.out.println(str);
            out.write(str.getBytes());
            out.close();
        } catch (Exception ex) {
            urlok=false;
            System.out.println("you sendMessage no!");
//            ex.printStackTrace();
        }
    }
    
    private String receiveMessage() throws Exception {
       
//     InputStream in=httpConnection.openInputStream();
//        String message=null;
//        StringBuffer sb = new StringBuffer();
//        int ch=0;
//        while((ch=in.read())!=-1)
//        {
//         sb.append((char)ch);
//        }
//        in.close();
//        message=sb.toString();
//        System.out.println(sb.toString());
//        return message;
                System.out.println("receiveMessage is run -->>"+(httpConnection));
        if(httpConnection == null || !urlok){
            System.out.println("httpConnection is null");
            return null;
        }

        //another method------------------------------------------------
        String message=null;
        if (httpConnection.getResponseCode() == HttpConnection.HTTP_OK) {
            InputStream in = httpConnection.openInputStream();
            int length = (int) httpConnection.getLength();
            if (length > 0) {
                byte servletData[] = new byte[length];
                in.read(servletData);
                in.close();
                //toString
                message = new String(servletData);
            } else
                message = "error!";
        }
        if(message == null){
            System.out.println("message is null");
            urlok=false;
        }
        System.out.println(message);
        return message;
    }

    private void close(){
        if(httpConnection!=null){
            try {
                httpConnection.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
   
    public void run() {
     urlok=true;
        String message = null;
        try {
//            System.out.println("run  0");
            loadCode();
//            System.out.println("run  1");
            connect(post_str);
//            System.out.println("run  2");
            if(urlok) {
                sendMessage(post_str);
//                System.out.println("run  3");
                message = receiveMessage();
                close();
//                System.out.println("run  4  ");
            }
        } catch (Exception ex) {
            System.out.println("run  error");
            ex.printStackTrace();
        }
        if(urlok)
        {
//            System.out.println("run  5");
            if(message==null){
                System.out.println("running message is null -->>");
            }
         int mycode_begin=message.indexOf("mycode=")+7 , mycode_end=message.indexOf("&myname");  
      int mypoint_begin=message.indexOf("mypoint=")+8 , mypoint_end=message.indexOf("&myrankings");
      int myname_begin=message.indexOf("myname=")+7 , myname_end=message.indexOf("&mypoint");
      int myrank_begin=message.indexOf("myrankings")+11 ,myrank_end=message.indexOf("&yname");
      
      int topname_begin=message.indexOf("topname=")+8,topname_end=message.indexOf("&toppoint");
      int toppoint_begin=message.indexOf("toppoint=")+9,toppoint_end=message.indexOf("&xname");
      
      int upname_begin=message.indexOf("xname=")+6,upname_end=message.indexOf("&xpoint");
      int uppoint_begin=message.indexOf("xpoint=")+7,uppoint_end=message.indexOf("&mycode");
      
      int downname_begin=message.indexOf("&yname=")+7,downname_end=message.indexOf("&ypoint");
      int downpoint_begin=message.indexOf("&ypoint=")+8;

      mystring[0]=message.substring(mycode_begin,mycode_end);//myCode
      mystring[1]=message.substring(mypoint_begin,mypoint_end);//my_point
      mystring[2]=message.substring(myname_begin,myname_end);//my_name
      mystring[3]=message.substring(myrank_begin,myrank_end);//my_ranking
      
      mystring[4]=message.substring(topname_begin,topname_end);//top_name
      mystring[5]=message.substring(toppoint_begin,toppoint_end);//top_point
      
      mystring[6]=message.substring(upname_begin,upname_end);//up_name
      mystring[7]=message.substring(uppoint_begin,uppoint_end);//up_point
      mystring[8]=String.valueOf((Integer.valueOf(mystring[3]).intValue())-1);//up_ranking
      
      mystring[9]=message.substring(downname_begin,downname_end);//down_name
      mystring[10]=message.substring(downpoint_begin);//down_point
      mystring[11]=String.valueOf((Integer.valueOf(mystring[3]).intValue())+1);//down_ranking
      
//      list.append("TOP:"+mystring[4]+"  "+mystring[5],null);//top
//      list.append(mystring[8]+":"+mystring[6]+"  "+mystring[7],null);//up
//      list.append(mystring[3]+":"+mystring[2]+"  "+mystring[1],null);//me
//      list.append(mystring[11]+":"+mystring[9]+"  "+mystring[10],null);//down
//                System.out.println("run  6");
      mycode=Integer.valueOf(mystring[0]).intValue();
//                System.out.println("run  7");
      saveData();
      sendOK=true;
        }
    }
   
    private static void saveData()
    {
        try
        {
            RecordStore rs = RecordStore.openRecordStore("code", true);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            DataOutputStream dos = new DataOutputStream(baos);          
            dos.writeInt(mycode);
            byte[] code_data = baos.toByteArray();
            baos.close();
            dos.close();
            rs.addRecord(code_data, 0, code_data.length);
//            rs.setRecord(2, data, 0, data.length);           
            rs.closeRecordStore();
            System.out.println("save data!---mycode="+mycode);
        }
        catch(Exception ex)
        {
            System.out.println("saveData  error");
            ex.printStackTrace();
        }
    }
   
    private void loadCode() {
        try {
            //Data->code
            RecordStore rs = RecordStore.openRecordStore("code", true);
            byte[] code_data = null;
            if (rs.getNumRecords() == 0) {
                rs.closeRecordStore();
//                return;
            } else {
                System.out.println("load code already!");
                code_data = rs.getRecord(1);
                ByteArrayInputStream bais = new ByteArrayInputStream(code_data);
                DataInputStream dis = new DataInputStream(bais);
                mycode = dis.readInt();
                bais.close();
                dis.close();
                rs.closeRecordStore();
                System.out.println("load mycode!---mycode="+mycode);
            }
           
            if(mycode==0)
                post_str="name="+myname+"&point="+mypoint+"&pam="+messageEncrypt(myname,""+mypoint);//123456";
            else
                post_str="code="+mycode+"&name="+myname+"&point="+mypoint+"&pam="+messageEncrypt(myname,""+mypoint);//123456";
           
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }
   
    private String messageEncrypt(String strName,String strPoint) throws Exception{
        System.out.println("messageEncrypt -->>  start");
        System.out.println(strName+"   "+strPoint);
        int i,nameIAsc,name = 0;
        char strNameI;
        String pam;
//*****************************************************
        for(i = 1;i <= strName.length();i++)    //name的字符串从第一个开始逐个取值
        {
            strNameI = strName.charAt(i-1);  //逐个取值
            nameIAsc = (int)(strNameI);   //把字符转成ascii码并转换成integer
            if (nameIAsc%3==2)       //ascii码除3余数是2的时候加26再乘于$i,不是2的时候加37乘于$i
            {
                nameIAsc = (nameIAsc+26)*i;
            }else {
                nameIAsc = (nameIAsc+37)*i;
            }
            name = name + nameIAsc;//102    //加每一个值
        }
        System.out.println("name= "+name);
//*********************************************************
        int pointIAsc,point = 0;
        char strPointI;
        for(i=1;i<=strPoint.length();i++)   //point的字符串从第一个开始逐个取值
        {
            strPointI = strPoint.charAt(i-1);  //逐个取值
            pointIAsc = (int)strPointI;   //把字符转成ascii码并转换成integer
            if(pointIAsc%2==1)       //ascii码奇数的时候加93再乘于$i,不是的时候加37乘于$i
            {
                pointIAsc = (pointIAsc+93)*i;
            }else {
                pointIAsc = (pointIAsc+71)*i;
            }
            point = point + pointIAsc;   //加每一个值
        }
 pam = (""+name).substring(0,2);//substr($name,0,2);  //name取前两个字符
 pam += (point/100)%10;//substr($point,-3,1); //跟上point倒数第三个数
 pam += (""+name).substring(2,3);//substr($name,2,1);  //跟上name第三个数
 pam += (point/10)%10;//substr($point,-2,1); //跟上point倒数每二个数
 pam += (name/10)%10;//substr($name,-2,1);  //跟上name倒数第二个数
 pam += (point)%10;//substr($point,-2,1); //跟上point倒数第一个数
       
        System.out.println(pam);
        System.out.println("messageEncrypt -->>  over");
 return pam;

    }
}

//使用/发送

else if(!RecordRanking && NetworkThread.urlok && keyCode == LEFT_SOFT_KEY){
                    scorePlace = -1;
                    if (createObject == null)
                        createObject = new CreateObject(this, 0);//GAME_RECORD
                    createObject.rmsTable(1);//GAME_RECORD
                    createObject = null;//GAME_RECORD
                   
                    netthread = new NetworkThread(recordScore[0],getRecordName(recordName[0]));
                    netthread.start();//启动
                    System.out.println("netthread.start() -->>  over "+NetworkThread.sendOK);
                    RecordRanking = true;
                }

 

///--------------绘制

                drawString_NewScreen(g,netthread.mystring[8]+"."+netthread.mystring[7], numberX, topY + 30 + 2 * 20,0);
                drawString_NewScreen(g,netthread.mystring[6], nameX, topY + 30 + 2 * 20,0);
                drawString_NewScreen(g,netthread.mystring[11]+"."+netthread.mystring[10], numberX, topY + 30 + 4 * 20,0);
                drawString_NewScreen(g,netthread.mystring[9], nameX, topY + 30 + 4 * 20,0);
                if(Integer.valueOf(netthread.mystring[3]).intValue() == 1){
                    drawString_NewScreen(g,">", numberX-3-(counter&1), topY + 30,Graphics.RIGHT);//箭头
                }

/// 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值