java SDK ,上传、读取 基本类型数据通过服务器(客户端代码)

来源:互联网 发布:java去除html代码 编辑:程序博客网 时间:2024/05/17 08:41

SDK共有两个类:

1)CVector

 

package dtutils;

public class CVector
{
  private int maxCount = 1;
  public Object[] object;
  protected int count = 0;

  public CVector()
  {
    this.object = new Object[this.maxCount];
  }

  public CVector(int size)
  {
    this.maxCount = size;

    this.object = new Object[this.maxCount];
  }

  public synchronized void setSize(int size)
  {
    if (size < this.count)
    {
      return;
    }
    if (this.count > 0)
    {
      Object[] ex = this.object;

      this.object = new Object[size];

      System.arraycopy(ex, 0, this.object, 0, this.count);
    }
    else
    {
      this.object = new Object[size];
    }

    this.maxCount = size;
  }

  public void refresh()
  {
    setSize(this.count);
  }

  public synchronized boolean addElement(Object o)
  {
    try
    {
      if (this.count >= this.maxCount)
      {
        setSize(this.maxCount + 1);
      }
      this.object[this.count] = o;

      this.count += 1;

      return true;
    }
    catch (Exception ex)
    {
    }

    return false;
  }

  public synchronized Object setElementAt(Object o, int index)
  {
    if ((index < 0) || (index >= this.count))
    {
      return null;
    }
    Object e = this.object[index];

    this.object[index] = o;

    return e;
  }

  public synchronized Object elementAt(int i)
  {
    if ((i < 0) || (i >= this.count))
    {
      return null;
    }
    return this.object[i];
  }

  public synchronized boolean insertElementAt(Object o, int i)
  {
    try
    {
      if (this.count >= this.maxCount)
      {
        setSize(this.maxCount + 1);
      }
      int index = i;

      if (index < 0)
      {
        index = 0;
      }
      if (index > this.count)
      {
        index = this.count;
      }
      System.arraycopy(this.object, index, this.object, index + 1, this.count - index);

      this.count += 1;

      return true;
    }
    catch (Exception ex)
    {
    }

    return false;
  }

  public synchronized int indexOf(Object o)
  {
    for (int rep = 0; rep < this.count; ++rep)
    {
      if (this.object[rep].equals(o))
      {
        return rep;
      }
    }
    return -1;
  }

  public synchronized void removeElementAt(int index)
  {
    if ((index < 0) || (index >= this.count))
    {
      return;
    }
    System.arraycopy(this.object, index + 1, this.object, index, this.count - index - 1);

    this.count -= 1;

    this.object[this.count] = null;
  }

  public synchronized void removeElement(Object o)
  {
    int i = indexOf(o);

    if (i == -1)
      return;
    removeElementAt(i);
  }

  public synchronized void removeAllElements()
  {
    for (int i = 0; i < this.count; ++i)
    {
      this.object[i] = null;
    }
    this.count = 0;
  }

  public int size()
  {
    return this.count;
  }

  public synchronized void copyInto(CVector e)
  {
    System.arraycopy(this.object, 0, e.object, 0, this.count);

    e.count = this.count;
  }

  public synchronized void copyInto(Object[] o)
  {
    System.arraycopy(this.object, 0, o, 0, this.count);
  }

  public Object[] toArray()
  {
    Object[] o = new Object[this.count];

    System.arraycopy(this.object, 0, o, 0, this.count);

    return o;
  }

  public boolean isEmpty()
  {
    return this.count == 0;
  }
}

 

2)DT

package dtutils;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.InputConnection;
import javax.microedition.io.OutputConnection;
import javax.microedition.midlet.MIDlet;

public class DT
{
  private static final int CMD_SAVE = 1000;
  private static final int CMD_LOAD = 1001;
  private MIDlet mid;
  private String host;
  private String userID;
  private String gameID;
  private String saveData;
  private String[] readData;

  public DT(MIDlet _mid)
  {
    this.mid = _mid;
    init();
  }

  private void init() {
    this.host = this.mid.getAppProperty("host");
    this.userID = this.mid.getAppProperty("userID");
    this.gameID = this.mid.getAppProperty("Game-ID");
    if (this.userID == null)
      this.userID = "9527";
  }

  public String getBackData(int index)
  {
    if ((this.readData == null) || (index < 0) || (index >= this.readData.length)) {
      return null;
    }
    return this.readData[(index + 3)];
  }

  public void ready()
  {
    this.saveData = "";
  }

  public boolean save() {
    try {
      return save(this.host);
    } catch (IOException ex) {
      ex.printStackTrace();
    }return false;
  }

  public boolean load()
  {
    try {
      return load(this.host);
    } catch (IOException ex) {
      ex.printStackTrace();
    }return false;
  }

  private boolean load(String url)
    throws IOException
  {
    System.out.println("load...");
    String base = "cmd=1001";
    base = base + "&userID=" + this.userID;
    base = base + "&gameID=" + this.gameID;

    HttpConnection c = getHttpConnection(url);
    DataOutputStream output = c.openDataOutputStream();
    byte[] ss = base.getBytes("UTF-8");
    output.write(ss);
    output.flush();
    DataInputStream input = c.openDataInputStream();

    StringBuffer b = new StringBuffer();
    while ((ch = input.read()) != -1)
    {
      int ch;
      b.append((char)ch);
    }
    String b2 = new String(b.toString().getBytes(), "UTF-8");
    System.out.println(b2);
    this.readData = getData(b2);

    return Integer.parseInt(this.readData[0]) == 1;
  }

  private boolean save(String url) throws IOException {
    System.out.println("save...");
    String base = "cmd=1000";
    base = base + "&userID=" + this.userID;
    base = base + "&gameID=" + this.gameID;
    base = base + "&" + this.saveData;
    System.out.println(base);

    HttpConnection c = getHttpConnection(url);
    DataOutputStream output = c.openDataOutputStream();
    byte[] ss = base.getBytes("UTF-8");
    output.write(ss);
    output.flush();
    DataInputStream input = c.openDataInputStream();

    StringBuffer b = new StringBuffer();
    while ((ch = input.read()) != -1)
    {
      int ch;
      b.append((char)ch);
    }

    String b2 = new String(b.toString().getBytes(), "UTF-8");
    System.out.println(b2);
    this.readData = getData(b2);
    return Integer.parseInt(this.readData[0]) == 1;
  }

  public void addPro(String name, String value)
  {
    this.saveData = (this.saveData + "&" + name + "=" + value);
  }

  public void addProInt(String name, int value) {
    this.saveData = (this.saveData + "&" + name + "=" + value);
  }

  private HttpConnection getHttpConnection(String url) {
    try {
      HttpConnection c = (HttpConnection)Connector.open(url, 3);
      c.setRequestMethod("POST");
      c.setRequestProperty("IF-Modified-Since", "15 Oct 2003 08:47:14 GMT");
      c.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");
      c.setRequestProperty("Content-Language", "en-CA");
      c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
      return c;
    } catch (IOException ex) {
      ex.printStackTrace();
    }
    return null;
  }

  public int getInt(String value)
  {
    return Integer.parseInt(value);
  }

  public void addProBoolean(String key, boolean value)
  {
    addPro(key, (value) ? "1" : "0");
  }
  public boolean getBoolean(String value) {
    int v = Integer.parseInt(value);
    return v == 1;
  }

  public void addProBooleanArray(String key, boolean[] value)
  {
    String v = "";
    for (int i = 0; i < value.length; ++i) {
      v = v + ((value[i] != 0) ? "1" : "0");
      if (i != value.length - 1) {
        v = v + ",";
      }
    }
    addPro(key, v);
  }
  public boolean[] getBooleanArray(String value) {
    int len = 1;
    for (int i = 0; i < value.length(); ++i) {
      if (value.charAt(i) == ',') {
        ++len;
      }
    }
    boolean[] v = new boolean[len];
    len = 0;
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < value.length(); ++i) {
      char c = value.charAt(i);
      if (c != ',') {
        sb.append(c);
      } else {
        int in = Integer.parseInt(sb.toString());
        v[len] = ((in == 1) ? 1 : false);
        ++len;
        sb.setLength(0);
      }
    }
    int in = Integer.parseInt(sb.toString());
    v[len] = ((in == 1) ? 1 : false);
    return v;
  }

  public void addProBooleanArray2(String key, boolean[][] value)
  {
    String v = "";
    for (int i = 0; i < value.length; ++i) {
      for (int j = 0; j < value[i].length; ++j) {
        v = v + ((value[i][j] != 0) ? "1" : "0");
        if ((i != value.length - 1) || (j != value[i].length - 1)) {
          v = v + ",";
        }
      }
    }
    addPro(key, v);
  }
  public boolean[][] getBooleanArray2(String value, int len1) {
    int len = 1;
    for (int i = 0; i < value.length(); ++i) {
      if (value.charAt(i) == ',') {
        ++len;
      }
    }
    boolean[][] v = new boolean[len1][len / len1];
    len = 0;
    StringBuffer sb = new StringBuffer();
    CVector vec = new CVector();
    for (int i = 0; i < value.length(); ++i) {
      char c = value.charAt(i);
      if (c != ',') {
        sb.append(c);
      } else {
        vec.addElement(sb.toString());
        sb.setLength(0);
      }
    }
    vec.addElement(sb.toString());
    len = 0;
    for (int i = 0; i < v.length; ++i) {
      for (int j = 0; j < v[i].length; ++j) {
        v[i][j] = ((Integer.parseInt((String)vec.elementAt(len)) == 1) ? 1 : 0);
        ++len;
      }
    }
    return v;
  }

  public void addProIntArray(String key, int[] value)
  {
    String v = "";
    for (int i = 0; i < value.length; ++i) {
      v = v + value[i];
      if (i != value.length - 1) {
        v = v + ",";
      }
    }
    addPro(key, v);
  }
  public int[] getIntArray(String value) {
    int len = 1;
    for (int i = 0; i < value.length(); ++i) {
      if (value.charAt(i) == ',') {
        ++len;
      }
    }
    int[] v = new int[len];
    len = 0;
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < value.length(); ++i) {
      char c = value.charAt(i);
      if (c != ',') {
        sb.append(c);
      } else {
        v[len] = Integer.parseInt(sb.toString());
        ++len;
        sb.setLength(0);
      }
    }
    v[len] = Integer.parseInt(sb.toString());
    return v;
  }

  public void addProIntArray2(String key, int[][] value)
  {
    String v = "";
    for (int i = 0; i < value.length; ++i) {
      for (int j = 0; j < value[i].length; ++j) {
        v = v + value[i][j];
        if ((i != value.length - 1) || (j != value[i].length - 1)) {
          v = v + ",";
        }
      }
    }
    addPro(key, v);
  }

  public int[][] getIntArray2Ex(String value, int len2)
  {
    int len = 1;
    for (int i = 0; i < value.length(); ++i) {
      if (value.charAt(i) == ',') {
        ++len;
      }
    }
    int[][] v = new int[len / len2][len2];
    len = 0;
    StringBuffer sb = new StringBuffer();
    CVector vec = new CVector();
    for (int i = 0; i < value.length(); ++i) {
      char c = value.charAt(i);
      if (c != ',') {
        sb.append(c);
      } else {
        vec.addElement(sb.toString());
        sb.setLength(0);
      }
    }
    vec.addElement(sb.toString());
    len = 0;
    for (int i = 0; i < v.length; ++i) {
      for (int j = 0; j < v[i].length; ++j) {
        v[i][j] = Integer.parseInt((String)vec.elementAt(len));
        System.out.println("" + v[i][j]);
        ++len;
      }
    }
    return v;
  }

  public int[][] getIntArray2(String value, int len1)
  {
    int len = 1;
    for (int i = 0; i < value.length(); ++i) {
      if (value.charAt(i) == ',') {
        ++len;
      }
    }
    int[][] v = new int[len1][len / len1];
    len = 0;
    StringBuffer sb = new StringBuffer();
    CVector vec = new CVector();
    for (int i = 0; i < value.length(); ++i) {
      char c = value.charAt(i);
      if (c != ',') {
        sb.append(c);
      } else {
        vec.addElement(sb.toString());
        sb.setLength(0);
      }
    }
    vec.addElement(sb.toString());
    len = 0;
    for (int i = 0; i < v.length; ++i) {
      for (int j = 0; j < v[i].length; ++j) {
        v[i][j] = Integer.parseInt((String)vec.elementAt(len));
        System.out.println("" + v[i][j]);
        ++len;
      }
    }
    return v;
  }

  private String[] getData(String s)
  {
    CVector v = new CVector();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < s.length(); ++i) {
      sb.append(s.charAt(i));
      if ((i < s.length() - 1) && (s.charAt(i + 1) == '#')) {
        v.addElement(sb.toString());
        sb.setLength(0);
      }
    }
    if (sb.length() > 0) {
      v.addElement(sb.toString());
    }
    String[] data = new String[v.size()];
    for (int i = 0; i < data.length; ++i) {
      String k = (String)v.elementAt(i);
      if (k.startsWith("#"))
        data[i] = k.substring(1, k.length());
      else {
        data[i] = k;
      }
    }

    return data;
  }
}

 

客户端API 使用demo:

 

package base;

import dtutils.DT;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
 * 寰楅�绉戞妧DTutils浣跨敤鑼冧緥
 * 20120610
 * @author colonleado
 */
public class RPY extends MIDlet{

    /** 鎸夐挳 */
    Command save, load;
    /** 闈㈡澘 */
    Form form;

    DT dt;

    protected void startApp() throws MIDletStateChangeException {
       
        // 鏂板缓涓�釜DT瀵硅薄 闇�浼犲叆midlet瀹炰緥浠ヨ幏鍙栭厤缃俊鎭�        dt = new DT(this);

        Display display = Display.getDisplay(this);
        form = new Form("DTuntilsDemo");
        save = new Command("save", Command.OK, 1);
        load = new Command("load", Command.BACK, 1);
        form.addCommand(save);
        form.addCommand(load);
        form.setCommandListener(new CommandListener() {
            public void commandAction(Command c, Displayable d) {
                if(c.equals(save)){
                    // 姣忔浣跨敤鍓嶈皟鐢╮eady鍒濆鍖栦竴涓�                    dt.ready();
                    // 娣诲姞瑕佸瓨妗g殑鏁版嵁 浠ey + value鐨勫舰寮�                    // key鍔″繀瑕佷笌鍙屾柟瑙勫畾鐨勬枃妗e唴涓�嚧 鍖呮嫭澶у皬鍐�                    dt.addProInt("curStage", 5);
                    dt.addProInt("curScore", 100000);
                    dt.addProBoolean("isDouble", true);
                    // 璋冪敤save鏂规硶杩涜瀛樻。 瀛樻。浠呰繑鍥炴垚鍔熸垨鑰呭け璐�                    if(dt.save()){
                        System.out.println("瀛樻。鎴愬姛");
                    }else{
                        System.out.println("瀛樻。澶辫触");
                    }
                }else if(c.equals(load)){
                    // 姣忔浣跨敤鍓嶈皟鐢╮eady鍒濆鍖栦竴涓�                    dt.ready();
                    // 璋冪敤load鏂规硶杩涜璇绘。 璇绘。杩斿洖鎴愬姛鎴栬�澶辫触 浠ュ強鏁版嵁
                    if(dt.load()){
                        System.out.println("璇绘。鎴愬姛");
                        // getBackData鏂规硶鎸夌収浣犵殑瀛樺偍椤哄簭鑾峰彇鏁版嵁 杩斿洖绫诲瀷涓哄瓧绗︿覆
                        // getInt getBoolean绛夋柟娉曞彧鏄负浜嗘柟渚夸娇鐢ㄥ皢瀛楃涓茶浆鎹负闇�鐨勭被鍨�涓嶆秹鍙婃牳蹇冧唬鐮�                        int curStage = dt.getInt(dt.getBackData(0));
                        int curScore = dt.getInt(dt.getBackData(1));
                        boolean isDouble = dt.getBoolean(dt.getBackData(2));
                        System.out.println("curStage = " + curStage);
                        System.out.println("curScore = " + curScore);
                        System.out.println("isDouble = " + isDouble);
                    }else{
                        System.out.println("璇绘。澶辫触");
                    }
                }
            }
        });
        display.setCurrent(form);
    }

    protected void pauseApp() {
    }

    protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
    }

}