有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

netbeans如何在javame中获取最新汇率数据

我是JavaMe新手,我想创建一个可以在线获取特定货币最新汇率的应用程序,我如何才能做到这一点

我已经能够创建HttpConnection对象并从URL检索数据,但它不适用于google

我从一个题为How to get a live exchange rate data and apply it to an Android App的问题中看到了一个例子here。然而,如果我在移动应用程序上尝试它,它将一无所获。 我怎样才能解决这个问题

String url = "http://www.google.com/ig/calculator?hl=en&q=1USD%3D%3FCAD";//test URL
c = (HttpConnection)Connector.open(url);
s = c.openInputStream();//open an input stream
if( l != -1 ) {
  for (i =0 ; i < l ; i++ )
  if((ch = s.read()) != -1){
     b.append((char) ch);
    }
}
stringItem2.setText("\n " + b.toString());

String url = "http://developers.sun.com/mobility/midp/articles/event/EventEx1.java";

我能够获得网站数据,但是

String url = "http://www.google.com/ig/calculator?hl=en&q=1USD%3D%3FCAD";

我什么也得不到


共 (2) 个答案

  1. # 1 楼答案

    请查找下面的代码,该代码返回json响应以获取转换率

        HttpClient client = new HttpClient();       
    
        NameValuePair arg1 = new NameValuePair("method","runJob");
    
        //Change your currency types here in which you would want to convert
    
        NameValuePair arg2 = new NameValuePair("from","USD");
        NameValuePair arg3 = new NameValuePair("to", "PKR");
    
        //getting the method
        GetMethod method = new GetMethod("http://rate-exchange.appspot.com/currency");
        method.setQueryString(new NameValuePair[]{arg1, arg2, arg3});
    
        //executes the link
        client.executeMethod(method);
    
        //getting response in string
        JSONObject obj = new JSONObject(method.getResponseBodyAsString());
    
        //getting rate from the json response
        double rate = obj.getDouble("rate");
    
        //closing conncetion
        method.releaseConnection();     
    
    
        //returning value
        return rate;
    
  2. # 2 楼答案

    我会使用http://xurrency.com/api/来检索最新的汇率,但是如果你每天查询超过10次,你必须为此支付29.99美元。页面的文档部分解释了如何使用API。例如,使用http://xurrency.com/api/usd/eur/1检索美元兑欧元汇率。 如果将其输入浏览器并查看源代码,您将看到检索到的响应。响应是JSON格式的

    您需要使用类似GSON的东西将JSON数据转换为Java对象。实现这一点的代码应该非常简单,比如:

        URL url = new URL("http://xurrency.com/api/usd/eur/1");
        URLConnection yc = url.openConnection();
        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                yc.getInputStream()));
        String jsonObject = "";
        String line;
        while ((line = in.readLine()) != null) 
            jsonObject += line;
    
        in.close();
        Gson gson = new Gson();
        ExchangeRate = gson.fromJson(jsonObject, ExchangeRate.class);
    

    ExchnageRate类将是一个定制类,用于封装从这个JSON API检索到的数据

    你可以让上面的代码与你正在使用的链接一起工作。请尝试以下代码:

      import com.google.gson.Gson;
      import java.io.BufferedReader;
      import java.io.InputStreamReader;
      import java.net.URL;
      import java.net.URLConnection;
    
      public class Main {
        public static void main(String[] args) throws Exception {
            URL url = new URL("http://www.google.com/ig/calculator?hl=en&q=1USD%3D%3FCAD");
            URLConnection yc = url.openConnection();
            BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                yc.getInputStream()));
            String jsonObject = "";
            String line;
            while ((line = in.readLine()) != null) 
                jsonObject += line;
    
            in.close();
            System.out.println(jsonObject);
            Gson gson = new Gson();
            GoogleCurrency another = gson.fromJson(jsonObject, GoogleCurrency.class);
            another.print();
        }
    
      }
    
    
      public class GoogleCurrency {
         private String lhs;
         private String rhs;
         private String error;
         private String icc;
    
       public GoogleCurrency() {
        lhs = "0 U.S Dollar";
        rhs = "0 U.S Dollar";
        error = "true";
        icc = "false";
       }
    
       public GoogleCurrency(String lhs,String rhs,String error,String icc) {
        this.lhs = lhs;
        this.rhs = rhs;
        this.error = error;
        this.icc = icc;
       }
    
       public void print() {
        System.out.println(lhs + " = " + rhs);
       }
     }