需要帮助android Json货币转换器

时间:2012-07-13 13:52:48

标签: android json

我有一个项目要求我建立一个货币转换器,我已经搜索谷歌并得到一些可以给我参考的来源 但是,当我点击计算它没有将textview更改为货币汇率时,我卡住了..

我使用了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的价值

1 个答案:

答案 0 :(得分:0)

我尝试了一项转换数据并在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);
        }
    }

}

我使用的布局xml文件

<?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>

使用您的微调器中的值替换URL,并在您的情况下使用DownloadData AsyncTask