2017-05-15 40 views
1

我的應用正試圖從雅虎財經獲得實時貨幣匯率。從Andriod studio的CSV文件中獲取信息?

以下是我的代碼,當我單擊按鈕時,總是返回0.1(API的返回值始終爲NULL)。我已經嘗試過我的java代碼,它的工作原理,但它不適用於Android Studio後,我粘貼我的代碼。

是閱讀csv文件或其他?

public class MainActivity extends AppCompatActivity { 

    public void startConvert(View view){ 
     EditText amount,from,to; 
     amount=(EditText)findViewById(R.id.amount); 
     from=(EditText)findViewById(R.id.from); 
     to=(EditText)findViewById(R.id.to); 
     Double many; 
     //many=Double.parseDouble(amount.toString()); 

     Toast.makeText(this,"haha",Toast.LENGTH_LONG).show(); 
     Double conv =findExchangeRateAndConvert("EUR", "USD", 1000); 

     Toast.makeText(this,Double.toString(conv),Toast.LENGTH_SHORT).show(); 
    } 

    private static Double findExchangeRateAndConvert(String from, String to, int amount) { 
     try { 
      //Yahoo Finance API 

      URL url = new URL("http://quote.yahoo.com/d/quotes.csv?s=" + from + to + "=X&f=l1&e=.csv"); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); 
      String line = reader.readLine(); 
      if (line.length() > 0) { 
       return Double.parseDouble(line) * amount; 
      } 
      reader.close(); 
     } catch (Exception e) { 
      System.out.println(e.getMessage()); 
     } 
     return 0.1; 
    } 
} 
+0

嘗試重新格式化您的代碼很難閱讀。此外,您可以更清楚地描述您的問題。 – DKIT

+0

問題是,當我嘗試從雅虎財務API獲得匯率時,它總是在不使用android studio的情況下返回null – Zhaoyuxuan

回答

1

試試這個..

 URL url = new URL("http://quote.yahoo.com/d/quotes.csv?s=" + from + to + "=X&f=l1&e=.csv"); 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpContext localContext = new BasicHttpContext(); 
     HttpGet httpGet = new HttpGet(url); 
     HttpResponse response = httpClient.execute(httpGet, localContext); 
     String result = ""; 
     InputStream is = response.getEntity().getContent(); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
     try { 
      String line; 
      while ((line = reader.readLine()) != null) { 
       String[] RowData = line.split(","); 
       date = RowData[0]; 
       value = RowData[1]; 
       // do something with "data" and "value" 
      } 
     } 
     catch (IOException ex) { 
      // handle exception 
     } 
     finally { 
      try { 
       is.close(); 
      } 
      catch (IOException e) { 
       // handle exception 
      } 
     } 
1

我會添加更多的URL參數的連接,我會保存到一個文件,以確保它不是問題:

URL url = new URL("http://quote.yahoo.com/d/quotes.csv?s=" + from + to + "=X&f=l1&e=.csv"); 
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
connection.setRequestProperty("Content-Type", "application/json"); 
connection.setRequestProperty("charset", "utf-8"); 
connection.setRequestMethod("GET"); 
DiS = connection.getInputStream(); 
BufferedReader rd = new BufferedReader(new InputStreamReader(DiS)); 
String data =""; 
StringBuilder sb = new StringBuilder(); 
while ((data=rd.readLine())!=null){ 
    sb.append(data); 
} 
data = sb.toString(); 
DiS.close(); 
connection.disconnect(); 
File mFile = new File(Environment.getExternalStorageDirectory(),"currency.csv"); 
if (!mFile.exists()) mFile.createNewFile(); 
FileWriter fw = new FileWriter(mFile); 
fw.write(data); 
fw.flush(); 
fw.close();