Android как прочитать CSV файл на удаленном сервере http в spinnere
Есть готовый пример, но отображает в textView, нужно отобразить выбранный элемент массива в Spinner и передать результат в переменную
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private TextView fileContent;
private static final String PATH_TO_SERVER = "https://";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fileContent = (TextView)findViewById(R.id.content_from_server);
Button loadTextButton = (Button)findViewById(R.id.load_file_from_server);
loadTextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DownloadFilesTask downloadFilesTask = new DownloadFilesTask();
downloadFilesTask.execute();
}
});
}
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){
printCVSContent(result);
}
}
}
private void printCVSContent(List<String[]> result){
String cvsColumn = "";
for (int i = 0; i < result.size(); i++){
String [] rows = result.get(i);
cvsColumn += rows[0] + " " + rows[1] + " " + rows[2] + "\n";
}
fileContent.setText(cvsColumn);
}
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.split(",");
csvLine.add(content);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
return csvLine;
}
}