【问题标题】:Android Currency ConversionAndroid 货币转换
【发布时间】:2012-12-05 19:14:19
【问题描述】:

可能是一个愚蠢的问题,但我希望将货币转换为例如所有美元。 我发现这是一个网络服务:http://www.webservicex.net/CurrencyConvertor.asmx?op=ConversionRate

我可以在 android 中使用它吗?如何使用?我应该请求转换率并获取新货币的金额或汇率,以便我可以使用它。

HTTP GET

The following is a sample HTTP GET request and response. The placeholders shown need to be replaced with actual values.

GET /CurrencyConvertor.asmx/ConversionRate?FromCurrency=string&ToCurrency=string HTTP/1.1
Host: www.webservicex.net
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<double xmlns="http://www.webserviceX.NET/">double</double>




HTTP POST

The following is a sample HTTP POST request and response. The placeholders shown need to be replaced with actual values.

POST /CurrencyConvertor.asmx/ConversionRate HTTP/1.1
Host: www.webservicex.net
Content-Type: application/x-www-form-urlencoded
Content-Length: length

FromCurrency=string&ToCurrency=string
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<double xmlns="http://www.webserviceX.NET/">double</double

> Blockquote

【问题讨论】:

    标签: android post get currency


    【解决方案1】:

    这应该相当容易。首先,您需要从 Web 请求文件,这可以使用常规 InputStreamReader 来完成,如 this 示例所示:

    URL url = new URL("http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency=USD&ToCurrency=GBP");
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(new InputSource(url.openStream()));
    doc.getDocumentElement().normalize();
    NodeList doubleList = doc.getElementsByTagName("double");
    string ratio = doubleList.item(0).getNodeValue();
    double dRatio = Double.ParseDouble(ratio);
    

    由此您可以得到两种货币之间的比率。

    【讨论】:

      【解决方案2】:

      谢谢托马斯!我最终通过雅虎管理!具有以下类:

      public class CurrencyConverter {
      
          static Context myContext = null;
          public double result;
          public String s;
          static AppicLifeService ALS;
      
      
          public CurrencyConverter (Context context) {
              myContext = context;
              ALS=new AppicLifeService(myContext);
      
      
               }
      
      
          public double ConvertCurrency (double amount, String from, String to){
              result=0;
              if (from==to){result=amount;} 
           else
           {
              try {
      
      
              s = getJson("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22"+from+to+"%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=");                      
      
              JSONObject jObj;
              jObj = new JSONObject(s);
              String exResult = jObj.getJSONObject("query").getJSONObject("results").getJSONObject("rate").getString("Rate");
      
              double exchangerate=Double.parseDouble(exResult);
      
              result=amount*exchangerate;
      
      
              } catch (JSONException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
                  ALS.Toast(myContext.getString(R.string.conversionerror), false);
              }
              catch (ClientProtocolException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
                  ALS.Toast(myContext.getString(R.string.conversionerror), false);
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
                  ALS.Toast(myContext.getString(R.string.conversionerror), false);
              }                                                    
          }
          return result;  
      
      }                               
      
      
      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();
      }
      }
      

      【讨论】:

        猜你喜欢
        • 2014-08-28
        • 2017-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-05
        • 2011-04-29
        相关资源
        最近更新 更多