我是Java ME新手,我想创建一个可以在线获取特定货币最新汇率的应用程序,我该如何完成?
我已经能够创建一个HttpConnection对象并从URL中检索数据,但它不适用于谷歌。
我在一个标题为here的问题中看到了一个示例How to get a live exchange rate data and apply it to an Android App。但是,如果我在移动应用程序上尝试它,它会返回nothings。 我该如何解决这个问题?
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";
我什么都没得到
答案 0 :(得分:1)
我会使用http://xurrency.com/api/来检索最新的汇率,如果您每天查询超过10次,则需要支付29.99。该页面的文档部分介绍了如何使用API。例如。要使用http://xurrency.com/api/usd/eur/1检索美国到EURO的兑换。 如果您在浏览器中输入此内容并查看源,您将看到检索到的响应。响应是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);
}
}
答案 1 :(得分:0)
请找到以下代码,该代码返回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;