【发布时间】:2012-07-13 13:52:47
【问题描述】:
我有一个项目要求我构建一个货币转换器,我已经搜索了谷歌并获得了一些可以给我参考的资源 但是当我点击计算时我卡住了它不会将文本视图更改为货币汇率..
我使用的是 json,这就是网站
http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20%28%22USDSGD%22%29&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=
这是部分代码
calculate.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
TextView texts = (TextView) findViewById(R.id.textView3);
try {
s = getJson("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22"+val[from]+val[to]+"%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=");
JSONObject jObj;
jObj = new JSONObject(s);
String theResult = jObj.getJSONObject("query").getJSONObject("results").getJSONObject("rate").getString("Rate");
texts.setText(theResult);
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
val[from]+val[to] 来自我的微调器,我有 2 个微调器
getJson 代码
public String getJson(String url)throws ClientProtocolException, IOException {
StringBuilder build = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String con;
while ((con = reader.readLine()) != null) {
build.append(con);
}
return build.toString();
}
请帮助我.. 这段代码哪里错了,我已经尝试编辑但什么也没得到.. 仍然当我点击按钮时,它没有给我 textview 的值
【问题讨论】:
-
应用程序是否在
logcat中抛出了某种Exception? -
查看 URL,您插入参数的方式似乎是错误的。如果对您有帮助,我推荐一个“更简单”的 API:google.com/ig/calculator?hl=en&q=1USD=?EUR 我同意@ninetwozero,因为您正在捕获异常,您应该能够在
logcat中看到它们打印到System.err -
您的代码没有使用任何类型的线程,所以只有延迟。
-
我试过你的代码......它正在我的文本视图中加载转换后的数据。正如
iNan指出的那样,由于您在 UI 线程本身中使用了它,因此存在延迟。 -
感谢您的帮助.. @Keaton Greve 我的朋友也要求我使用它,但如果我使用它,我仍然可以使用 json 吗?我尝试使用 jsoup,但我不知道如何将价值从 android 放到那个网站我只知道如何获取价值,你能教我怎么做吗?