2012-12-05 2 views
0

어리석은 질문 일지 모르지만 통화를 예를 들어 모든 달러로 변환하고 싶습니다. 웹 서비스로 이것을 발견했습니다 : http://www.webservicex.net/CurrencyConvertor.asmx?op=ConversionRateAndroid 통화 변환

안드로이드 및이를 어떻게 사용할 수 있습니까? 전환율을 요청하고 새 통화 또는 금액으로 금액을 받아야하므로 사용할 수 있습니다.

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 

답변

1

이것은 상당히 쉬워야합니다. 첫째로 당신은 웹에서 파일을 요청해야하며, this 예에서와 같이이 정기적 InputStreamReader를 사용하여 수행 할 수 있습니다 :이에서

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); 

당신은 다음 두 통화 사이의 비율을 얻을 수 있습니다.

0

감사합니다. Thomas! 나는 결국 Yahoo!를 통해 관리했다. 다음 클래스로 구성 :

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(); 
} 
}