Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading .txt file from shared linked from dropbox Android studio

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.

like image 941
Henry Avatar asked Jan 21 '16 15:01

Henry


2 Answers

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:

  1. MainActivity - It includes a 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.
  2. DownloadFragment - It is a retained fragment that wraps the 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.
  3. FileContentAdapter - It is the adapter used for displaying file content inside the RecyclerView.

Some Limitations

  • This application is not focused on Dropbox. In case file is public on internet you can download it regardless of who is hosting it. In case your purpose is keeping the activity automatically synchronized with file on Dropbox it would be probably better to exploit Dropbox SDK, in particular if you are planning to access files that are private on Dropbox.
  • AsyncTask implementation should be improved, for example by implementing WakeLock management.
like image 97
thetonrifles Avatar answered Nov 05 '22 07:11

thetonrifles


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();
                }
            });

        }
    }
};

}

like image 2
Gueorgui Obregon Avatar answered Nov 05 '22 07:11

Gueorgui Obregon