从Dropbox链接获取元数据而不使用身份验证

时间:2013-09-03 15:39:08

标签: java android sync dropbox dropbox-api

我想检查一个版本已更改/获取文本文件的元数据,并在dropbox上显示共享链接。我不会使用dropbox api,因为它会让用户使用自己的帐户。我希望他们链接到我的帐户,我不能手动执行此操作,因为我可能会在以后更改密码。

所以:没有身份验证令牌,只是从dropbox的共享链接获取元数据,以便我可以检查版本更改以及版本是否已更改下载新文件的内容。

另外:我也愿意接受其他建议来完成这项工作。请详细解释一下您的解决方案。

更新了电子标签问题:

public void getFromOnlineTxtDatabase(){
        try{
            URL url = new URL("url-here");
            HttpURLConnection.setFollowRedirects(true);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setDoOutput(false);
            con.setReadTimeout(20000);
            con.setRequestProperty("Connection", "keep-alive");
            //get etag for update check
                String etag = con.getHeaderField("etag");
            //String etag= "";

            con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0");
            ((HttpURLConnection) con).setRequestMethod("GET");
            //System.out.println(con.getContentLength()) ;
            con.setConnectTimeout(5000);
            BufferedInputStream in = new BufferedInputStream(con.getInputStream());
            int responseCode = con.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                System.out.println(responseCode);
            }
            StringBuffer buffer = new StringBuffer();
            int chars_read;
            //int total = 0;
            while ((chars_read = in.read()) != -1) 
            {
                char g = (char) chars_read;
                buffer.append(g);
            }
            final String page = buffer.toString();
            //create password_ems.txt to internal
            if (fileExistance("data.txt")){
                File dir = getFilesDir();
                File file = new File(dir, "data.txt");
                boolean deleted = file.delete();
                stringToTxt(page, "data.txt");


            }else{
                stringToTxt(page, "data.txt");
            }

            if (fileExistance("data_etag.txt")){
                File dir = getFilesDir();
                File file = new File(dir, "etag.txt");
                boolean deleted = file.delete();
                stringToTxt(etag, "etag.txt");


            }else{
                //create etag_file
                stringToTxt(etag, "data_etag.txt");
            }

            //  Log.i("Page", page);
        }catch(Exception e){
            showDialog("Database Fetch Failure","Unable to Fetch Password Database, check your internet" +
                    " connection and try again later.",0);
            Log.i("Page", "Error");
        }

    }

1 个答案:

答案 0 :(得分:3)

如果针对公共或共享Dropbox网址发出HTTP HEAD请求,您将获得etag标头。我不知道这种行为是有保证的,因为我认为它没有记录在任何地方,但至少现在etag标头可以用来确定文件何时发生了变化。 (如果etag不同,则文件已更改。)

修改

通常,在使用ETag时,最有效的方法是发出标头为GET的{​​{1}}请求。如果内容未更改,则将以304响应,但如果内容已更改,则将按照正常的If-None-Match: <old etag>请求下载新内容(响应将为200)。