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

📄 currencyexchange.java

📁 JAVA编程百例书中各章节的所有例子的源代码,包括套接字编程
💻 JAVA
字号:
package ch09.section04;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
import java.lang.*;
import java.util.*;

public class CurrencyExchange
    extends MIDlet
    implements CommandListener {

//退出按钮
  private Command exitCommand;

  private Display display;

  Form displayForm;

  public CurrencyExchange() {
    display = Display.getDisplay(this);
    exitCommand =
        new Command("Exit", Command.SCREEN, 1);

  }

//启动应用程序,创建窗体,打开连接
  public void startApp() {
    displayForm = new Form("Exchange Rate");
    displayForm.addCommand(exitCommand);
    displayForm.setCommandListener(this);

    try {

      String result = getViaHttpConnection
          ("http://finance.yahoo.com/m5?a=1&s=USD&t=GBP");
      displayForm.append(" " + result);

    }
    catch (Exception exc) {
      exc.printStackTrace();
    }
    display.setCurrent(displayForm);
  }

//挂起应用程序
  public void pauseApp() {}

//销毁应用程序
  public void destroyApp(boolean unconditional) {}

//响应退出按钮事件
  public void commandAction(
      Command c, Displayable s) {
    if (c == exitCommand) {
      destroyApp(false);
      notifyDestroyed();
    }

  }

  String parse(String str) {
//获取相关金融信息
    int timeIndex = str.indexOf("U.S. Markets Closed.");
    String retTime = "";
    if (timeIndex != -1) {
      retTime = str.substring(timeIndex - 34, timeIndex);
      retTime += " U.S. Markets Closed ";
    }

    String retVal = "";
    int dec = 0;

    int index = str.indexOf("USDGBP");
    if (index != -1) {
      str = str.substring(index, str.length());

    }
    if ( ( (dec = str.indexOf(".")) != -1) && (! (str.endsWith(".")))
        && Character.isDigit(str.charAt(dec + 1))) {
      String front = "";
      int find = dec - 1;
      while (Character.isDigit(str.charAt(find))) {
        front += str.charAt(find);
        find--;
      }
      retVal += new StringBuffer(front).reverse().toString();
      retVal += ".";

      String back = "";
      int bind = dec + 4;
      while (Character.isDigit(str.charAt(bind))) {
        back += str.charAt(bind);
        bind--;
      }
      retVal += new StringBuffer(back).reverse().toString();

    }
    System.out.println(retVal);

    return "USD/GBP " + retVal + " at " + retTime;
  }

  String getViaHttpConnection(String url) throws IOException {
    HttpConnection c = null;
    InputStream is = null;
    StringBuffer str = new StringBuffer();

    try {
      c = (HttpConnection) Connector.open(url);

//获取当前内容类型
      String type = c.getType();

// requested.打开输入流,用于读取HTTP头
      is = c.openInputStream();

//获取信息长度
      int len = (int) c.getLength();
      int ch;
      while ( (ch = is.read()) != -1) {
        str.append( (char) ch);
      }

    }
    finally {
      if (is != null) {
        is.close();
      }
      if (c != null) {
        c.close();
      }
    }

//解析字符串,获取汇率
    String val = parse(str.toString());
    System.out.println(val);
    return val;
  }
}

⌨️ 快捷键说明

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