I tried to load the csv file from server and using the data to draw graph in my Android app.
Here is the code
private class DownloadFilesTask extends AsyncTask<URL, Void, List<String[]>> {
protected List<String[]> doInBackground(URL... urls) {
return downloadRemoteTextFileContent();
}
protected void onPostExecute(List<String[]> result) {
if(result != null){
createLineGraph(result);
}
}
}
private void createLineGraph(List<String[]> result){
DataPoint[] dataPoints = new DataPoint[result.size()];
for (int i = 0; i < result.size(); i++){
String[] rows = result.get(i);
Log.d(TAG, "Output " + Integer.parseInt(rows[0].trim()) + " " + Integer.parseInt(rows[1].trim()));
dataPoints[i] = new DataPoint(Integer.parseInt(rows[0].trim()), Integer.parseInt(rows[1].trim()));
}
LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(dataPoints);
mGraph.addSeries(series);
}
private List<String[]> downloadRemoteTextFileContent(){
URL mUrl = null;
List<String[]> csvLine = new ArrayList<>();
String[] content = null;
try {
mUrl = new URL(PATH_TO_SERVER);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
assert mUrl != null;
URLConnection connection = mUrl.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line = "";
while((line = br.readLine()) != null){
content = line.trim().split(",");
csvLine.add(content);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
return csvLine;
}
However, the parseInt function in createLineGraph function return me this error
java.lang.NumberFormatException: For input string: "1"
I have no idea why the string read from csv file cannot parse into Integer. Please help!