如何将json数据打印到textview中

时间:2014-06-13 11:32:29

标签: android json

在我的应用中,我尝试通过JSON访问api,使用api将金额从一种货币转换为另一种货币并使用下面的代码段。
但是onClick没有返回任何内容,也没有给出任何错误! 我做错了什么?
请帮忙。

private static final String API_URL = "http://devel.farebookings.com/api/curconversor/t1.toString()/t2.toString()/1/json";// PUT

                public String t1="";                                                                                                                    

                public String t2="";    

btnDoit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (!usdValue.getText().toString().equals("")) {
                    AsyncHttpClient client = new AsyncHttpClient();
                    client.get(API_URL, new AsyncHttpResponseHandler() {


                        @Override
                        public void onSuccess(String response) {
                            Log.i("CHACHING", "HTTP Sucess");

                            try {
                                JSONObject jsonObj = new JSONObject(response);
                                JSONObject ratesObject = jsonObj
                                        .getJSONObject("data");

                                Double RateFrom = ratesObject.getDouble("t1.toString()");
                                Double RateTo = ratesObject.getDouble("t2.toString()");


                                Double usds = Double.valueOf(usdValue.getText()
                                        .toString());

                                Double gbps = usds * RateFrom;
                                Double euros = usds * RateTo;
                                Result.setText("ConvertedFrom: "
                                        + String.valueOf(gbps)+ "ConvertedTo: "
                                                + String.valueOf(euros));
                                //ConvertedTo.setText("ConvertedTo: "
                                        //+ String.valueOf(RateTo));

                            } catch (JSONException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                        }
                    });
                } else {
                    Toast.makeText(getApplicationContext(),
                            "Please enter a Amount value!", Toast.LENGTH_LONG)
                            .show();

                }

            }
        });

    }

    public void onItemSelected(AdapterView<?> parent, View view, int position,
            long id) {


        codey = ConvertedFromCurr.getSelectedItem().toString();
        codey1 = ConvertedToCurr.getSelectedItem().toString();

        code = codey.split("\\s+");

        for (int i = 0; i < code.length; i++) {


   t1= code[1].toString();
   Toast.makeText(parent.getContext(),t1.toString(),
             Toast.LENGTH_LONG).show();
            //Code.append("" + code[1].toString() + "")     

}


        code1 = codey1.split("\\s+");

        for (int i = 0; i < code1.length; i++) 
        {


   t2= code1[1].toString();
   Toast.makeText(parent.getContext(), t2.toString(),
             Toast.LENGTH_LONG).show();
            //Code.append("" + code[1].toString() + "");

        }

    }

1 个答案:

答案 0 :(得分:0)

http://devel.farebookings.com/api/curconversor/t1.toString()/t2.toString()/1/json是无效的网址。虽然你应该得到JSON异常。但是,如果将url更改为有效的url,则仍然无法在JSON响应中获取“data”对象。最后,logcat可能会崩溃。