所以我对 Android 比较陌生,我正在尝试解析基于 Web 的 CSV 文档,并在我的应用程序中使用该文档中的两个值。我已经成功解析了一个 CSV 文档,但它只有 1 行。我试图解析的文档如下所示:
Light,2012-08-20T11:04:42.407301Z,107
Temperature,2012-08-20T11:04:42.407301Z,24
我正在尝试获取“107”和“24”值。谁能解释如何做到这一点?这是我当前的 CSV 解析器类的代码,它可以成功解析一行 CSV 数据。
public class CSVParser {
static InputStream is = null;
private String value;
public String getCSV(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
try {
String line;
while ((line = reader.readLine()) != null) {
String[] RowData = line.split(",");
value = RowData[2];
// do something with "data" and "value"
}
} catch (IOException ex) {
// handle exception
} finally {
try {
is.close();
} catch (IOException e) {
// handle exception
}
}
return value;
}
}