I am trying to read a .txt file from dropbox that has a public shared link. What I want to do is read this .txt and display all the data inside this file on a listview in android.
http://txt.do/5zflt (I don't have access to drop on my current computer so I want to use this link as an example)
The file is called PersonStatus that will contains text something along the lines of;
Online
Offline
Active
Holidays
….
….
…
…
…
…
basically what I want to do is used the shared dropbox link to read this text and display it in my listview on android but I am not sure how I can approach this. I have searched online for tutorials and guides but being new to android I haven't been able to find something of much use;
For example, I found this link: Read a file from dropbox where the OP has asked a similar question but has not provided enough code for me to understand how I can approach this. Also through my research I found that dropbox has Android Sync API: https://www.dropbox.com/developers-v1/sync/start/android but being new to programming I am not quite sure how to go about implementing and making it work.
I would really appreciate if anyone can help. Thanks in advance. If my question was not clear please let me know and I will try explaining it better.
I put here on GitHub a sample project implementing scenario you described (I also put a public file with structure you reported here on Dropbox). Inside project you will find the following main components:
RecyclerView
that will be populated with file content rows. In order to get file content, the activity relies on a retained fragment, allowing download task to be kept in case of screen rotation (see here for details on configuration changes). File is downloaded automatically as soon as activity is created, but you can force re-download by using SYNC button on the action bar. AsyncTask
used for downloading file. It provides a Callback
implemented by the MainActivity
for handling specific events occurring during download (e.g. onPrepare, onProgress, onDownloadCompleted, onDownloadFailed). You can use them for example for showing a progress bar or other feedbacks to user.RecyclerView
.Some Limitations
AsyncTask
implementation should be improved, for example by implementing WakeLock
management. In my apps i use this code to get content of a shared dropbox file. I call this code inside of AsyncTask.
Edited: Here is a sample
public class DropboxSampleActivity extends Activity {
private ListView listViewDropbox;
private ArrayAdapter<String> adapter = null;
private static String URL_FILE_DROPBOX = "https://www.dropbox.com/s/xxxxxxxxxxxx/xxxxxxxxxxxx?dl=1";
private ArrayList<String> listElementItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_dropbox_list);
super.onCreate(savedInstanceState);
listViewDropbox = (ListView) findViewById(R.id.listViewDropbox);
DropboxItemAsyncTask dropboxItemAsyncTask = new DropboxItemAsyncTask();
dropboxItemAsyncTask.execute();
}
class DropboxItemAsyncTask extends AsyncTask {
protected Integer doInBackground(Object[] params) {
try {
listElementItem = new ArrayList<>();
URLConnection conn = new URL(URL_FILE_DROPBOX).openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"), 8);
String line = null;
while ((line = reader.readLine()) != null) {
listElementItem.add(line);
}
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Object o) {
if (adapter == null) {
adapter = new ArrayAdapter(DropboxSampleActivity.this,
android.R.layout.simple_list_item_1, listElementItem);
runOnUiThread(new Runnable() {
@Override
public void run() {
listViewDropbox.setAdapter(adapter);
}
});
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
adapter.notifyDataSetChanged();
}
});
}
}
};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With