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

从dropbox Android studio的共享链接读取.txt文件,android,listview,dropbox,dropbox-api,readfile,Android,Listview,Dropbox,Dropbox Api,Readfile,我正在尝试从dropbox读取一个带有公共共享链接的.txt文件。我想做的是读取这个.txt文件,并在android的listview中显示该文件中的所有数据 (我无法访问当前计算机上的drop,因此我想使用此链接作为示例) 该文件称为PersonStatus,它将包含沿以下行的文本: Online Offline Active Holidays …. …. … … … … 基本上,我想做的是使用共享dropbox链接来阅读此文本并在android上的listview中显示它,但我不确定如何实

我正在尝试从dropbox读取一个带有公共共享链接的.txt文件。我想做的是读取这个.txt文件,并在android的listview中显示该文件中的所有数据

(我无法访问当前计算机上的drop,因此我想使用此链接作为示例)

该文件称为PersonStatus,它将包含沿以下行的文本:

Online
Offline
Active
Holidays
….
….
…
…
…
…
基本上,我想做的是使用共享dropbox链接来阅读此文本并在android上的listview中显示它,但我不确定如何实现这一点。我在网上搜索过教程和指南,但由于不熟悉安卓系统,我还没有找到什么有用的东西

例如,我发现了这样一个链接:OP问了一个类似的问题,但没有提供足够的代码让我理解如何处理这个问题。同样通过我的研究,我发现dropbox有Android同步API:但作为编程新手,我不太确定如何实现并使其工作


如果有人能帮忙,我将不胜感激。提前谢谢。如果我的问题不清楚,请让我知道,我会尝试更好地解释。

在我的应用程序中,我使用此代码获取共享dropbox文件的内容。我在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();
                }
            });

        }
    }
};
公共类DropboxSampleActivity扩展活动{
私有ListView listViewDropbox;
专用ArrayAdapter适配器=null;
私有静态字符串URL\u文件\u DROPBOX=”https://www.dropbox.com/s/xxxxxxxxxxxx/xxxxxxxxxxxx?dl=1";
私有ArrayList列表项;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
setContentView(R.layout.activity\u dropbox\u列表);
super.onCreate(savedInstanceState);
listViewDropbox=(ListView)findViewById(R.id.listViewDropbox);
DropboxItemAsyncTask DropboxItemAsyncTask=新建DropboxItemAsyncTask();
dropboxItemAsyncTask.execute();
}
类DropboxItemAsyncTask扩展了AsyncTask{
受保护的整数doInBackground(对象[]参数){
试一试{
listElementItem=new ArrayList();
URLConnection conn=新URL(URL\u FILE\u DROPBOX).openConnection();
连接();
InputStream is=conn.getInputStream();
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
为“UTF-8”)、8);
字符串行=null;
而((line=reader.readLine())!=null){
listElementItem.add(行);
}
is.close();
}捕获(例外e){
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(对象o){
if(适配器==null){
适配器=新阵列适配器(DropboxSampleActivity.this,
android.R.layout.simple_list_item_1,listlementItem);
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
listViewDropbox.setAdapter(适配器);
}
});
}否则{
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
adapter.notifyDataSetChanged();
}
});
}
}
};
}

我在GitHub上放了一个实现您描述的场景的示例项目(我还放了一个公共文件,其中的结构是您在Dropbox上报告的)。在项目内部,您将发现以下主要组件:

  • MainActivity-它包括一个将填充文件内容行的
    RecyclerView
    。为了获取文件内容,活动依赖于保留的片段,允许在屏幕旋转时保留下载任务(有关配置更改的详细信息,请参阅)。创建“活动”后,文件将自动下载,但您可以使用操作栏上的“同步”按钮强制重新下载
  • DownloadFragment-它是一个保留的片段,用于包装用于下载文件的
    AsyncTask
    。它提供由
    main活动实现的
    回调
    ,用于处理下载过程中发生的特定事件(例如onPrepare、onProgress、ondownloadpleted、ondownloadfiled)。例如,您可以使用它们向用户显示进度条或其他反馈
  • FileContentAdapter-它是用于在
    RecyclerView
    中显示文件内容的适配器
  • 一些限制

    • 此应用程序不关注Dropbox。若这个文件在互联网上是公开的,你们可以下载它,不管它是谁托管的。如果您的目的是让活动自动与Dropbox上的文件同步,那么利用Dropbox SDK可能会更好,特别是如果您计划访问Dropbox上的私有文件
    • AsyncTask
      应改进实施,例如通过实施
      WakeLock
      管理
    您好,很抱歉回复太晚。我不确定AsyncTask是如何工作的,也不确定我如何着手编写一个。请帮我一把。另外,当你用这个从dropbox中读取文件时,它是每秒都在读取还是只读取一次,然后在下次更新文件/添加更多文本时才读取。阅读完后如何显示文本,我将在listview中显示它。你也在做类似的事情吗?嗨,我发现这很难实现。你能帮忙吗?嗨@g2o,我又试过这个代码了,但似乎无法正常工作。请您提供一个示例项目。是否可以添加一个功能,将从drop-on用户电话读取的文件中的数据保存为缓存,然后只查找dropbox文件中的更新,因为这将允许用户访问信息,而无需internet合作