【问题标题】:Get the information from CSV file in Android studio?从 Android Studio 中的 CSV 文件中获取信息?
【发布时间】:2017-05-15 09:52:34
【问题描述】:

我的应用正在尝试从 yahoo Finance 获取实时货币汇率。

以下是我的代码,当我点击按钮时,总是返回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;
    }
}

【问题讨论】:

  • 尝试重新格式化难以阅读的代码。此外,在描述您的问题时,您可能会更清楚。
  • 问题是,当我尝试从 yahoo Finance API 获取汇率时,它在不使用 android studio 的情况下工作时总是返回 null

标签: java android api csv yahoo-finance


【解决方案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
            }
        }

【讨论】:

    【解决方案2】:

    我会在 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();
    

    【讨论】:

      猜你喜欢
      • 2021-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-15
      • 2017-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多