2012-12-18 30 views
5

Trong đơn đăng ký của tôi, tôi cần nhận tỷ giá hối đoái hiện tại cho các loại tiền tệ khác nhau. Vì nó đã được đề xuất bởi this, thisthis câu hỏi một cách hay để làm điều đó, là sử dụng dịch vụ Tài chính của Yahoo.Tỷ giá ngoại tệ của Yahoo trong Android 4

Vì vậy, khi tôi muốn tìm, ví dụ, tỷ lệ giữa đô la Mỹ và đô la Canada, tôi chỉ cần gửi link này: http://download.finance.yahoo.com/d/quotes.csv?s=USDCAD=X&f=sl1d1t1ba&e=.csv

này đang làm việc tốt cho cả điện thoại Motorola Atrix của tôi với Android 2.3.4 và giả lập lại với Google API 2.3.3. Tuy nhiên, khi tôi thử liên kết chính xác tương tự từ Galaxy SII với Android ICS 4.0 và trình giả lập với Google API 4.0, trong cả hai trường hợp tệp quotes.csv chỉ chứa "Danh sách ký tự bị thiếu".

Sau khi tìm hiểu xung quanh, tôi phát hiện ra rằng phản ứng này có thể xảy ra trong trường hợp không tìm thấy giá. Nhưng phản hồi này là cho bất kỳ loại tiền tệ nào tôi thử dưới Android 4.0 (Galaxy SII hoặc trình giả lập). Do đó, tôi không thể có được giá với Android 4.0, nhưng tôi có thể với Android 2.x.

Có ai đã gặp phải sự cố tương tự không? Có bất kỳ công việc xung quanh?

EDIT: Đây là mã đề mà những giao dịch với tốc độ tải về từ Yahoo Service tệ:

//show the progress dialog 
downloadingDialog.show(); 
Runnable getRates = new Runnable() { 
    public void run(){ 
     dataNotFound = false; 
     final String baseDir = getApplicationContext().getFilesDir().getAbsolutePath(); 
     //download the rates from yahoo to a CSV file 
     downloadFileViaHTTP (baseDir); 
     //read the file line 
     String filePath = baseDir + "/" + "quotes.csv"; 
     Log.d(tag, "-> filePath = " + filePath); 
     try { 
     // open the file for reading 
     InputStream instream = new FileInputStream(filePath); 
     // if file the available for reading 
     if (instream != null) { 
      // prepare the file for reading 
      InputStreamReader inputreader = new InputStreamReader(instream); 
      BufferedReader buffreader = new BufferedReader(inputreader); 
      //read the line 
      String fileLine = buffreader.readLine(); 
      Log.d(tag, "fileLine = " + fileLine); 
      instream.close(); 
     } 
     } 
     catch (Exception ex) { 
     // print stack trace. 
     } 

    } 
}; 
final Thread t = new Thread(getRates); 
t.start(); 

Và đây là chức năng của tôi để tải về các tập tin quotes.csv từ trang Yahoo:

public void downloadFileViaHTTP (String localPath) { 
    Log.d(tag, "downloadFileViaHTTP..."); 

    try { 
     //this is the Yahoo url 
     String urlFile = "http://download.finance.yahoo.com/d/quotes.csv?s=" + fromCurrency + toCurrency + "=X&f=sl1d1t1ba&e=.csv"; 
     Log.d(tag,"urlFile = " + urlFile); 
     URL url = new URL(urlFile); 
     //create the new connection 
     HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
     urlConnection.setRequestMethod("GET"); 
     urlConnection.setDoOutput(true); 
     urlConnection.connect(); 

     //pointer to the downloaded file path 
     String localFileName = localPath + "/" + "quotes.csv"; 
     //this is the actual downloaded file 
     File MyFilePtrDest = new File(localFileName); 
     Log.d(tag,"localFileName = " + localFileName); 

     //this will be used in reading the data from the Internet 
     InputStream inputStream = urlConnection.getInputStream(); 

     //this will be used to write the downloaded data into the file we created 
     FileOutputStream fileOutput = new FileOutputStream(MyFilePtrDest); 

     byte[] buffer = new byte[1024]; 
     int bufferLength = 0; //used to store a temporary size of the buffer 

     //write buffer contents to file 
     while ((bufferLength = inputStream.read(buffer)) > 0) { 
     //add the data in the buffer to the file in the file output stream (the file on the sd card 
     fileOutput.write(buffer, 0, bufferLength); 
     } 

     inputStream.close(); 
     //close the output stream when done 
     fileOutput.flush(); 
     fileOutput.close(); 
     urlConnection.disconnect(); 
    } 
    catch (IOException e) { 
     //data were not found 
     dataNotFound = true; 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

Đây là nhật ký từ API Google 2.3.3 giả lập:

12-18 11:04:24.091: D/CurrencyConverter(414): downloadFileViaHTTP... 
12-18 11:04:24.091: D/CurrencyConverter(414): urlFile = http://download.finance.yahoo.com/d/quotes.csv?s=EURUSD=X&f=sl1d1t1ba&e=.csv 
12-18 11:04:24.282: D/CurrencyConverter(414): localFileName = /data/data/com.myapps.currencyconverter/files/quotes.csv 
12-18 11:04:24.461: D/CurrencyConverter(414): -> filePath = /data/data/com.myapps.currencyconverter/files/quotes.csv 
12-18 11:04:24.461: D/CurrencyConverter(414): fileLine = "EURUSD=X",1.3172,"12/18/2012","4:03am",1.317,1.3174 

Và đây là các bản ghi từ API 4.0 giả lập Google:

12-18 11:47:36.130: D/CurrencyConverter(668): downloadFileViaHTTP... 
12-18 11:47:36.130: D/CurrencyConverter(668): urlFile = http://download.finance.yahoo.com/d/quotes.csv?s=EURUSD=X&f=sl1d1t1ba&e=.csv 
12-18 11:47:36.449: D/dalvikvm(668): GC_CONCURRENT freed 306K, 4% free 11714K/12167K, paused 5ms+10ms 
12-18 11:47:36.951: D/CurrencyConverter(668): localFileName = /data/data/com.myapps.currencyconverter/files/quotes.csv 
12-18 11:47:37.159: D/CurrencyConverter(668): -> filePath = /data/data/com.myapps.currencyconverter/files/quotes.csv 
12-18 11:47:37.159: D/CurrencyConverter(668): fileLine = Missing Symbols List. 

Như bạn có thể nhìn thấy "fileLine" Chuỗi biến, trong trường hợp đầu tiên nó được tỷ lệ thích hợp, trong khi đồng thứ hai, các tập tin quotes.csv chỉ chứa "Thiếu danh sách ký hiệu". giá trị.

EDIT2: Tôi đã tải lên một dự án Android hoàn chỉnh shared folder, để mọi người có thể dùng thử. Bạn có thể biên dịch và chạy với cả Android 2.x và 4 trình giả lập (hoặc điện thoại nếu bạn có :-))

EDIT3: Mặc dù đây không phải là câu trả lời trực tiếp, nhưng đó vẫn là giải pháp thay thế. Tôi sử dụng máy tính tiền tệ của Google thay vì máy tính tiền của Yahoo. Để sử dụng máy tính tiền tệ của Google, chỉ cần thay đổi url Yahoo bằng số này: http://www.google.com/ig/calculator?hl=en&q=1 "+ fromCurrency +" =? "+ ToCurrency. Nhấp vào this link để xem ví dụ về chuyển đổi 78 Euro thành USD. Tôi đã kiểm tra và nó hoạt động với cả hai phiên bản Android Tuy nhiên, nếu có ai phát hiện ra lý do tại sao điều này xảy ra với trang web của Yahoo, thì tốt nhất là nên chia sẻ với chúng tôi ..

+0

gửi mã của bạn. –

+0

tôi đã chỉnh sửa câu trả lời của tôi (EDIT3) với một giải pháp sử dụng máy tính tiền tệ của Google. – Dimitris

Trả lời

1

Hãy thử xóa urlConnection.setDoOutput(true); khi bạn chạy trong ICS. Vì ICS chuyển yêu cầu GET thành POST khi setDoOutput (true).

vấn đề này được báo cáo herehere

+0

Có! Đây là vấn đề! Cảm ơn rất nhiều! – Dimitris

Các vấn đề liên quan