티스토리 뷰

728x90


Dropbox API 등록하기 참고자료
      http://thdev.net/40 

Dropbox API 사용 Upload
      http://thdev.net/21  

Dropbox API 사용 Download
      http://thdev.net/22  

Dropbox에서 정의되어 있는 metadata에 대한 설명입니다.

/metadata

The metadata API location provides the ability to retrieve file and folder metadata and manipulate the directory structure by moving or deleting files and folders.

URL Structure
https://api.dropbox.com/<version>/metadata/dropbox/<path>
https://api.dropbox.com/<version>/metadata/sandbox/<path>
Version(s)
0
Method(s)
GET
Parameter(s)
  • callback - Optional. The server will wrap its response inside a call to the argument specified by callback. Value must contain only alphanumeric characters and underscores.
  • file_limit - Optional. Default is 10000. When listing a directory, the service will not report listings containing more than file_limit files and will instead respond with a 406 (Not Acceptable) status response.
  • hash - Optional. Listing return values include a hash representing the state of the directory's contents. If you provide this argument to the metadata call, you give the service an opportunity to respond with a "304 Not Modified" status code instead of a full (potentially very large) directory listing. This argument is ignored if the specified path is associated with a file or if list=false.
  • list - Optional. The strings true and false are valid values. true is the default. If true, this call returns a list of metadata representations for the contents of the directory. If false, this call returns the metadata for the directory itself.
  • status_in_response - Optional. Some clients (e.g., Flash) cannot handle HTTP status codes well. If this parameter is set to true, the service will always return a 200 status and report the relevant status code via additional information in the response body. Default is false.
Returns

metadata returns metadata for the file or directory at the given <path> location relative to the user's Dropbox or the user's application sandbox. If <path> represents a directory and the list parameter is true, the metadata will also include a listing of metadata for the directory's contents.

Sample JSON return value for a file:

{
"thumb_exists": false,
"bytes": 579538,
"modified": "Tue, 04 Nov 2008 02:52:28 +0000",
"path": "/Public/aim_guide.pdf",
"is_dir": false,
"size": "566.0KB",
"root": "dropbox",
"icon": "page_white_acrobat"
}

Sample JSON return value for a directory when list parameter is set to true (if list is false the contents key will simply be omitted from the result):

{
"hash": "528dda36e3150ba28040052bbf1bfbd1",
"thumb_exists": false,
"bytes": 0,
"modified": "Sat, 12 Jan 2008 23:10:10 +0000",
"path": "/Public",
"is_dir": true,
"size": "0 bytes",
"root": "dropbox",
"contents": [
{
    "thumb_exists": false,
    "bytes": 10,
    "modified": "Tue, 03 May 2011 12:21:12 +0000",
    "path": "/Public/sample.txt",
    "icon": "page_white",
    "size": "10 bytes",
    "is_dir": false
}
],
"icon": "folder_public"
}

Return Value Definitions

  • thumb_exists - Returns true if the file is an image and Dropbox has a thumbnail on hand. If true, use the thumbnails call to retrieve the thumbnail.
  • icon - Denotes the name of the icon used to illustrate the file type. Typically, Dropbox will use an icon in the file browser to show the file type. Dropbox uses icons from the Silk Icons library. Download Dropbox's version of the icon library.
Error Codes
304 Directory contents have not changed (relies on 'hash' parameter).
400 Operation attempted not allowed by token type. Root parameter is not full access or Sandbox.
404 File path not found.
406 Too many file entries to return.

문서에는 Error Codes와 JSON으로 넘겨지는 값, URL, Parameter 값 모두 정의되어 있습니다.

이 모든것을 한번에 처리 할 수 있도록 android dropbox 라이브러리에는 아래와 같이 한줄로 처리 할 수 있습니다.
Entry entry = api.metadata("dropbox", "호출 경로", 10000, null, true);
와 같습니다.
1. 역시 기존 다운, 업로드와 마찬가지로 dropbox, sandbox 두가지 분류로 되어 있지만 개인이 사용하기 위한 dropbox는 dropbox라는 코드로만 전송하면 됩니다.
2. 2번째 코드 역시 dropbox내의 폴더 경로를 보여주게 됩니다. 하위경로를 알고 싶으면 위와 같이 작성하면 됩니다.
   가장 최상위 폴더 경로인 root를 알고 싶다면 "/" 렇게만 작성하면 root 아래의 모든 파일 폴더 정보를 가져 올 수 있습니다.
   dropbox api에 모두 정의되어 있습니다. Android 상에서 단순히 리스트로 뿌려주거나 파일 입출력을 통해서 테스트 해볼 수 있습니다.
3. 파일 리밋 = 가져올 파일의 수의 최대 값을 정 할 수 있습니다. 기본값은 10000 입니다. (api를 통해서 불러오기 때문에 0을 삽입하면 호출 되지 않습니다.)
4. hash 파일 또는 목록 명을 주지 않으면 false로 처리됩니다.
5. true로 처리하게되면 서비스 상태를 항상 200으로 호출 하게 되고, body부분에 오류가 난 정보를 넘겨주게 됩니다. 필요할 경우에 true를 하시면 됩니다. 기본값은 역시 false로 처리되어 있습니다.

dropbox 문서에 자세한 설명이 나와 있습니다. 빈약한 ;; 해석으로 오류가 있을 수 있으니 오류가 있는 부분은 댓글로 남겨주시면 됩니다.!!^^';;

---- 아래는 리스트뷰에 뿌려주기 위한 코드입니다. 단순 테스트를 위해 작성한 코드이기 때문에 실제 적용하는 대에는 오류가 있을 수 있습니다.

package com.yoon.DropBox;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import com.dropbox.client.DropboxAPI.Entry;
import android.content.Context;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

//Dropbox activity를 상속 받습니다.
public class DropboxList extends DropboxSample {
    //ArrayList Entry와 Entry를 선언합니다.
    ArrayList<Entry> entrylist;
    Entry entry;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);
        
        ListView list = (ListView)findViewById(R.id.list);
        //Entry를 가지기위해서 api.metadata를 호출 합니다.(dropbox 라이브러리에서 entry로 return값이 정의되어 있습니다.
        entry = api.metadata("dropbox", "/qnote_list/나의_메모/", 10000, null, true);
        //ArrayList<Entry>에 entry를 복사합니다.(Entry클래스 안에 처리되어 있습니다.)
        entrylist = entry.contents;
        //Log.d("TA", "" + entrylist.size());
        
        Adapter adapter = new Adapter(this, 0, entrylist);
        list.setAdapter(adapter);
        
        //리스트의 onItemClickListener 이용하여 다운로드를 할 수 있습니다.
        list.setOnItemClickListener(new OnItemClickListener() {
            
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
            //    download(entry.path, entrylist.get(arg2).path);
            }
        });
    }
    
//adapter를 만듭니다.
    class Adapter extends ArrayAdapter<Entry> {
        View v;
        ViewHolder holder;

        public Adapter(Context context, int textViewResourceId, List<Entry> objects) {
            super(context, 0, objects);
            // TODO Auto-generated constructor stub
        }
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            
            if(convertView == null) {
                LayoutInflater inflater = LayoutInflater.from(getContext());
                v = inflater.inflate(R.layout.list_item_1, null);
                holder = new ViewHolder();
                holder.name = (TextView) v.findViewById(R.id.text);
            } else {
                v = convertView;
                holder = (ViewHolder) v.getTag();
            }
            
            Entry entry = getItem(position);
            //파일 경로를 리스트에 뿌려줍니다.
            //entry안에 모두 정의되어 있으니 위의 문서를 참고하여 필요한 리스트를 뿌려주시면 됩니다.
            //참고로 Dropbox에서 제공되는 시간은 모두 수정 시간을 제공합니다. 수정 시간을 현재 휴대폰의 시간과 비교하여 몇분 후 몇 초후로 나타내고 싶다면 아래를 참고해주세요.
            holder.name.setText(entry.fileName());
            return v;
        }
    }
    
    class ViewHolder {
        TextView name;
    }
}

//Entry에 넘어온 modified를 현재 휴대폰 상의 시간과 비교하기
            //아래와 같은 시간으로 넘어오게 됩니다. format을 아래와 같이 해주면 dropbox에서 사용하는 시간과 동일하게 처리됩니다.
            //Tue, 23 Aug 2011 14:44:47 +0000
            SimpleDateFormat format
                 = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);
            long cratedAtMillis = 0;
            String time = null;
            try {
                Date date = format.parse(entry.modified);
                cratedAtMillis = date.getTime();
                Log.d("TA", "createdAtMillis : " + cratedAtMillis);
                //현재시간을 기준으로 얼마나 지났는지 체크하여 String으로 나타내어 줍니다.
                time = (String) DateUtils.getRelativeTimeSpanString(cratedAtMillis, System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS);
            } catch (ParseException e) {
                Log.e("Twitter", "Parse Exception" + e.getMessage());
            }
            Log.d("TA", time);


//당연히 sd카드를 사용하기 위해서는 메니패스트파일에 퍼미션을 등록해야 합니다!!
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />





댓글