2011-07-06 4 views
-1

자바 ME에 새로운, 그리고 paticular 통화에 대한 최신 환율을 온라인으로 얻을 수있는 앱을 만들고 싶습니다. 어떻게해야합니까?어떻게하면 최신 환율 데이터를 java-me에서 얻을 수 있습니까

HttpConnection 개체를 만들고 URL에서 데이터를 검색 할 수 있지만 Google에서는 작동하지 않습니다.

여기서 How to get a live exchange rate data and apply it to an Android App이라는 제목의 질문에서 here을 보았습니다. 그러나, 내가 모바일 애플 리케이션에 그것을 시도하면 nothings를 반환합니다. 어떻게 해결할 수 있습니까? 내가 웹 사이트 데이터를 얻을 수 있어요

String url = "http://developers.sun.com/mobility/midp/articles/event/EventEx1.java"; 

String url = "http://www.google.com/ig/calculator?hl=en&q=1USD%3D%3FCAD";//test URL 
c = (HttpConnection)Connector.open(url); 
s = c.openInputStream();//open an input stream 
if(l != -1) { 
    for (i =0 ; i < l ; i++) 
    if((ch = s.read()) != -1){ 
    b.append((char) ch); 
    } 
} 
stringItem2.setText("\n " + b.toString()); 

하지만

String url = "http://www.google.com/ig/calculator?hl=en&q=1USD%3D%3FCAD"; 

와 나는까지 간 검색 http://xurrency.com/api/을 사용 아무것도

+0

확인할 사항이 있습니까? 아니면 우리가 당신을 위해 그것을 적어주기를 원합니까? – SJuan76

+0

무엇이 작동하지 않습니까? 어디서 환율 데이터를 가져 왔습니까? 도움이 필요하면 코드를 보여주십시오. –

답변

0

를 얻을 환율을 하루에 10 회 이상 조회하는 경우 29.99를 지불해야합니다. 이 페이지의 문서 부분에는 API 사용 방법이 설명되어 있습니다. 예 : 유로 교환을 위해 미국을 검색하려면 http://xurrency.com/api/usd/eur/1을 사용하십시오. 브라우저에 이것을 입력하고 소스를 보면 응답을 검색 할 수 있습니다. 응답은 JSON에 있습니다.

JSON 데이터를 Java 객체로 변환하려면 GSON과 같은 것을 사용해야합니다.

URL url = new URL("http://xurrency.com/api/usd/eur/1"); 
    URLConnection yc = url.openConnection(); 
    BufferedReader in = new BufferedReader(
          new InputStreamReader(
          yc.getInputStream())); 
    String jsonObject = ""; 
    String line; 
    while ((line = in.readLine()) != null) 
     jsonObject += line; 

    in.close(); 
    Gson gson = new Gson(); 
    ExchangeRate = gson.fromJson(jsonObject, ExchangeRate.class); 

ExchnageRate 클래스는이 JSON의 API에서 검색 한 데이터를 캡슐화하기위한 사용자 정의 클래스가 될 것입니다 : 코드는,이 매우 간단해야 뭔가를 할 수 있습니다.

위의 코드를 사용중인 링크와 함께 사용할 수 있습니다. 다음 코드를 사용해보십시오 :

import com.google.gson.Gson; 
    import java.io.BufferedReader; 
    import java.io.InputStreamReader; 
    import java.net.URL; 
    import java.net.URLConnection; 

    public class Main { 
    public static void main(String[] args) throws Exception { 
     URL url = new URL("http://www.google.com/ig/calculator?hl=en&q=1USD%3D%3FCAD"); 
     URLConnection yc = url.openConnection(); 
     BufferedReader in = new BufferedReader(
          new InputStreamReader(
          yc.getInputStream())); 
     String jsonObject = ""; 
     String line; 
     while ((line = in.readLine()) != null) 
      jsonObject += line; 

     in.close(); 
     System.out.println(jsonObject); 
     Gson gson = new Gson(); 
     GoogleCurrency another = gson.fromJson(jsonObject, GoogleCurrency.class); 
     another.print(); 
    } 

    } 


    public class GoogleCurrency { 
    private String lhs; 
    private String rhs; 
    private String error; 
    private String icc; 

    public GoogleCurrency() { 
    lhs = "0 U.S Dollar"; 
    rhs = "0 U.S Dollar"; 
    error = "true"; 
    icc = "false"; 
    } 

    public GoogleCurrency(String lhs,String rhs,String error,String icc) { 
    this.lhs = lhs; 
    this.rhs = rhs; 
    this.error = error; 
    this.icc = icc; 
    } 

    public void print() { 
    System.out.println(lhs + " = " + rhs); 
    } 
} 
+0

괜찮습니다. 이 말은 Google에서이 작업을 수행 할 수 없다는 것을 의미합니까? 훨씬 쉽게 볼 수 있기 때문에? – TiOLUWA

+0

Google 계산기는 본 적이 없지만 정상적으로 작동합니다. 앱에서이 URL의 데이터를 사용하는 것이 Google에서 설정 한 이용 약관을 위반하는지 여부는 문제입니다. – GordyD

+0

좋아, 내가 사용하는 용어는 구글과 함께 문제가 전에 들었어요,이 접근 방식은 나를 위해, 고마워요, 내 문제를 해결 괜찮아요 작동합니다. – TiOLUWA

0

이 전환율을 얻기를위한 JSON 응답을 반환 아래의 코드를 찾아주세요.

HttpClient client = new HttpClient();  

    NameValuePair arg1 = new NameValuePair("method","runJob"); 

    //Change your currency types here in which you would want to convert 

    NameValuePair arg2 = new NameValuePair("from","USD"); 
    NameValuePair arg3 = new NameValuePair("to", "PKR"); 

    //getting the method 
    GetMethod method = new GetMethod("http://rate-exchange.appspot.com/currency"); 
    method.setQueryString(new NameValuePair[]{arg1, arg2, arg3}); 

    //executes the link 
    client.executeMethod(method); 

    //getting response in string 
    JSONObject obj = new JSONObject(method.getResponseBodyAsString()); 

    //getting rate from the json response 
    double rate = obj.getDouble("rate"); 

    //closing conncetion 
    method.releaseConnection();  


    //returning value 
    return rate; 
관련 문제