需要帮助的Android Json的汇率兑换 [英] need help android Json Currency Converter
问题描述
我有一个项目,问我要建立一个货币转换器,我已经搜索谷歌并得到了一些源代码,可以给我一个参考
但我被困当我点击计算它不TextView的改变为货币的汇率。
我使用JSON,这是该网站
<$p$p><$c$c>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=这是一些code的
calculate.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
TextView的文本=(的TextView)findViewById(R.id.textView3); 尝试{
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 =新的JSONObject(S);
字符串theResult = jObj.getJSONObject(查询)getJSONObject(结果)getJSONObject(速度)的getString(速度)。; texts.setText(theResult);
} 赶上(JSONException E){
// TODO自动生成catch块
e.printStackTrace();
}
赶上(ClientProtocolException E){
// TODO自动生成catch块
e.printStackTrace();
}赶上(IOException异常五){
// TODO自动生成catch块
e.printStackTrace();
}
}
}
瓦尔[从] + VAL [来]是来自我的微调,我有2个微调
在的getJSON code
公共字符串的getJSON(字符串URL)抛出ClientProtocolException,IOException异常{
StringBuilder的构建=新的StringBuilder();
HttpClient的客户端=新DefaultHttpClient();
HTTPGET HTTPGET =新HTTPGET(URL);
HTT presponse响应= client.execute(HTTPGET);
HttpEntity实体= response.getEntity();
InputStream的内容= entity.getContent();
读者的BufferedReader =新的BufferedReader(新的InputStreamReader(内容));
字符串CON;
而((CON = reader.readLine())!= NULL){
build.append(CON);
}
返回build.toString();
}
请帮助我..这是哪里code的错,我已经尝试编辑并没有什么..还当我点击它不按钮给我的价值的TextView
我试过,转换数据并将其显示在一个TextView活动
进口java.io.BufferedReader中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.InputStreamReader中;进口org.apache.http.HttpEntity;
进口org.apache.http.Htt presponse;
进口org.apache.http.client.ClientProtocolException;
进口org.apache.http.client.HttpClient;
进口org.apache.http.client.methods.HttpGet;
进口org.apache.http.impl.client.DefaultHttpClient;
进口org.json.JSONException;
进口org.json.JSONObject;进口android.app.Activity;
进口android.app.ProgressDialog;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;
进口android.widget.TextView;公共类CurrencyActivity延伸活动{ 私人TextView的文本; @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); 的setContentView(R.layout.test); Button按钮=(按钮)findViewById(R.id.calculateCmd);
button.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(视图v){
新DownloadData()执行();
}
}); 文=(的TextView)findViewById(R.id.txtCurrency); } 公共字符串的getJSON(字符串URL)抛出ClientProtocolException,
IOException异常{
StringBuilder的构建=新的StringBuilder();
HttpClient的客户端=新DefaultHttpClient();
HTTPGET HTTPGET =新HTTPGET(URL);
HTT presponse响应= client.execute(HTTPGET);
HttpEntity实体= response.getEntity();
InputStream的内容= entity.getContent();
读者的BufferedReader =新的BufferedReader(新的InputStreamReader(
内容));
字符串CON;
而((CON = reader.readLine())!= NULL){
build.append(CON);
}
返回build.toString();
} 类DownloadData扩展的AsyncTask&LT;无效,整数,字符串&GT; { ProgressDialog PD = NULL; @覆盖
在preExecute保护无效(){
super.on preExecute();
PD =新ProgressDialog(CurrencyActivity.this);
pd.setTitle(转换...);
pd.setMessage(请稍候...);
pd.setCancelable(假);
pd.show(); } @覆盖
保护字符串doInBackground(虚空...... PARAMS){ 字符串s;
串theResult =;
尝试{
S = getJson(\"http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDSGD%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=\");
的JSONObject jObj;
jObj =新的JSONObject(S);
theResult = jObj.getJSONObject(查询)
.getJSONObject(结果)。getJSONObject(速度)
.getString(速度); 的System.out.println(theResult);
} 赶上(JSONException E){
e.printStackTrace();
}赶上(ClientProtocolException E){
e.printStackTrace();
}赶上(IOException异常五){
e.printStackTrace();
} 返回theResult;
} @覆盖
保护无效onPostExecute(字符串theResult){
super.onPostExecute(theResult);
pd.dismiss(); 的System.out.println(theResult:+ theResult);
texts.setText(theResult);
}
}}
我已经使用的布局xml文件
&LT;?XML版本=1.0编码=UTF-8&GT?;
&LT;的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT&GT; &LT;的LinearLayout
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerInParent =真
机器人:方向=垂直
机器人:比重=中心
&GT; &LT;按钮
机器人:ID =@ + ID / calculateCmd
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=计算/&GT; &LT;的TextView
机器人:ID =@ + ID / txtCurrency
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=收入&GT;
&LT; / TextView的&GT;
&LT; / LinearLayout中&GT;&LT; / RelativeLayout的&GT;
从纺纱替换值的URL,然后在你的情况使用DownloadData的AsyncTask
I have a project that ask me to build a currency converter, i already search google and got some source that can give me a reference but i got stuck when i click calculate it doesnt change the textview to the rate of the currency..
I using a json and this is the site
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=
this is some of the code
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();
}
}
}
the val[from]+val[to] is come from my spinner, i have 2 spinner
the getJson code
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();
}
please help me.. where is the wrong of this code i already try to edit and got nothing .. still when i click the button it dont give me the value to textview
i tried an activity that converts the data and displays it in a textview
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class CurrencyActivity extends Activity {
private TextView texts;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
Button button = (Button) findViewById(R.id.calculateCmd);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DownloadData().execute();
}
});
texts = (TextView) findViewById(R.id.txtCurrency);
}
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();
}
class DownloadData extends AsyncTask<Void, Integer, String> {
ProgressDialog pd = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(CurrencyActivity.this);
pd.setTitle("Converting...");
pd.setMessage("Please wait...");
pd.setCancelable(false);
pd.show();
}
@Override
protected String doInBackground(Void... params) {
String s;
String theResult = "";
try {
s = getJson("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDSGD%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=");
JSONObject jObj;
jObj = new JSONObject(s);
theResult = jObj.getJSONObject("query")
.getJSONObject("results").getJSONObject("rate")
.getString("Rate");
System.out.println(theResult);
}
catch (JSONException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return theResult;
}
@Override
protected void onPostExecute(String theResult) {
super.onPostExecute(theResult);
pd.dismiss();
System.out.println("theResult:" + theResult);
texts.setText(theResult);
}
}
}
the layout xml file i have used
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical"
android:gravity="center"
>
<Button
android:id="@+id/calculateCmd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate" />
<TextView
android:id="@+id/txtCurrency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Income" >
</TextView>
</LinearLayout>
</RelativeLayout>
replace the URL with the values from your spinners and use the DownloadData AsyncTask in your case
这篇关于需要帮助的Android Json的汇率兑换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!