从 dropbox Android studio 的共享链接中读取 .txt 文件

Reading .txt file from shared linked from dropbox Android studio

我正在尝试从具有 public 共享 link 的保管箱中读取 .txt 文件。我想要做的是阅读此 .txt 并在 android.

中的列表视图中显示此文件中的所有数据

http://txt.do/5zflt(我无法在我当前的计算机上访问 drop,所以我想使用此 link 作为示例)

该文件名为 PersonStatus,其中包含类似于以下内容的文本;

Online
Offline
Active
Holidays
….
….
…
…
…
…

基本上我想做的是使用共享保管箱 link 阅读此文本并将其显示在 android 上的列表视图中,但我不确定如何处理此问题。我在网上搜索了教程和指南,但我是 android 的新手,没能找到有用的东西;

例如,我发现了这个 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 但作为编程新手,我不太确定如何着手实施并使其发挥作用。

如果有人能提供帮助,我将不胜感激。提前致谢。如果我的问题不清楚,请告诉我,我会尽力解释得更好。

在我的应用程序中,我使用此代码获取共享保管箱文件的内容。我在 AsyncTask 中调用这段代码。

已编辑:这是一个示例

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

        }
    }
};

}

我把 here on GitHub a sample project implementing scenario you described (I also put a public file with structure you reported here 放在了 Dropbox 上)。在项目中,您会发现以下主要组件:

  1. MainActivity - 它包含一个 RecyclerView,它将填充文件内容行。为了获取文件内容,activity 依赖于保留的片段,允许在屏幕旋转的情况下保留下载任务(有关配置更改的详细信息,请参阅 here)。创建 activity 后会自动下载文件,但您可以使用操作栏上的 SYNC 按钮强制重新下载。
  2. DownloadFragment - 这是一个保留的片段,包装了用于下载文件的 AsyncTask。它提供了一个由 MainActivity 实现的 Callback 来处理下载期间发生的特定事件(例如 onPrepare、onProgress、onDownloadCompleted、onDownloadFailed)。例如,您可以使用它们向用户显示进度条或其他反馈。
  3. FileContentAdapter - 它是用于在 RecyclerView.
  4. 中显示文件内容的适配器

一些限制

  • 此应用程序不专注于 Dropbox。如果文件在 Internet 上 public,您可以下载它,而不管它是由谁托管的。如果您的目的是保持 activity 与 Dropbox 上的文件自动同步,那么利用 Dropbox SDK 可能会更好,特别是如果您打算访问 Dropbox 上的私有文件。
  • AsyncTask 实施应得到改进,例如实施 WakeLock 管理。