2017-05-15 25 views
1

私のアプリはyahoo financeからライブ通貨為替レートを取得しようとしています。Andriod studioのCSVファイルから情報を取得しますか?

私のコードは、ボタンをクリックすると0.1が常に返されます(APIからの戻り値は常にNULLです)。私は自分のコードを貼り付けた後、私は自分のJavaコードを試して、それは動作しますが、それはAndroidスタジオで動作していません。

それはcsvファイルまたはsth elseを読み込むの問題ですか?

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から為替レートを取得しようとすると、アンドロイドスタジオを使わずに作業している間は常にnullを返すことです – Zhaoyuxuan

答えて

1

私がURL接続に多くのパラメータを追加したいと私はそれは問題ではないことを確認するために、ファイルに保存したい

 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 = 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(); 
関連する問題