从 Android studio 中的 CSV 文件获取信息?
Get the information from CSV file in Android studio?
我的应用正在尝试从雅虎财经获取实时货币汇率。
以下是我的代码,当我单击按钮时,0.1
总是 returned(API 中的值 return 总是 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;
}
}
试试这个..
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
}
}
我会向 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();
我的应用正在尝试从雅虎财经获取实时货币汇率。
以下是我的代码,当我单击按钮时,0.1
总是 returned(API 中的值 return 总是 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;
}
}
试试这个..
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
}
}
我会向 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();