如何在JavaME中获取最新的汇率数据
我是JavaMe新手,我想创建一个可以在线获取特定货币最新汇率的应用程序,我如何才能做到这一点 我已经能够创建HttpConnection对象并从URL检索数据,但它不适用于google 我在这里看到了一个问题的例子,题为。然而,如果我在移动应用程序上尝试它,它将一无所获。 我怎样才能解决这个问题如何在JavaME中获取最新的汇率数据,java,netbeans,java-me,Java,Netbeans,Java Me,我是JavaMe新手,我想创建一个可以在线获取特定货币最新汇率的应用程序,我如何才能做到这一点 我已经能够创建HttpConnection对象并从URL检索数据,但它不适用于google 我在这里看到了一个问题的例子,题为。然而,如果我在移动应用程序上尝试它,它将一无所获。 我怎样才能解决这个问题 String url = "http://www.google.com/ig/calculator?hl=en&q=1USD%3D%3FCAD";//test URL c = (HttpCon
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://www.google.com/ig/calculator?hl=en&q=1USD%3D%3FCAD";
我没有得到任何东西我会用它来检索最新的汇率,但如果你每天查询超过10次,你必须为此支付29.99英镑。页面的文档部分解释了如何使用API。例如,检索您使用的美元兑欧元汇率。
如果您将其输入浏览器并查看源,您将看到检索到的响应。响应是JSON格式的
您需要使用类似的方法将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);
}
}
请查找下面的代码,该代码返回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;
没有要检查的代码?或者你只是想让我们帮你写下来?什么不管用?你从哪里获得汇率数据?如果你需要帮助,请告诉我们代码。好的,非常感谢。这是否意味着谷歌不可能做到这一点,因为它看起来简单得多?我以前从未见过谷歌计算器,但它会工作得很好。这只是一个将此URL中的数据用于你的应用程序是否违反了谷歌规定的使用条款的问题。好的,我听说谷歌之前就存在使用条款问题,这种方法对我很有效,非常感谢,你解决了我的问题。
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;