Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
567 views
in Technique[技术] by (71.8m points)

upload - Using Dropbox Java API for uploading files to dropbox

I want to upload files using the java API for DropBox. The following code gets me the oauth_token and oauth_secret. but when ever I try to upload a file I get a exception.
Java Class

package com.dropbox.client;

import com.dropbox.client.DropboxAPI.Config;
import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Charan
 */
public class DBmain {

    public DBmain() {
        System.setProperty("java.net.useSystemProxies", "true");
        Map configuration= new HashMap();
        configuration.put("consumer_key", "XXXXXXXXXXXXXXXX");
        configuration.put("consumer_secret", "XXXXXXXXXXXXXXXX");
        configuration.put("request_token_url", "http://api.dropbox.com/0/oauth/request_token");
        configuration.put("access_token_url", "http://api.dropbox.com/0/oauth/access_token");
        configuration.put("authorization_url", "http://api.dropbox.com/0/oauth/authorize");
        configuration.put("port",80);
        //configuration.put("trusted_access_token_url","http://api.getdropbox.com/0/token");
        configuration.put("server","api.getdropbox.com");
        configuration.put("content_server","api-content.dropbox.com");
        String username="myMailAddress@domain.com";
        String password="myPassword";
        try {
           Authenticator auth = new Authenticator(configuration);
           String url = auth.retrieveRequestToken("");
           String access_key = auth.getTokenKey();
           String access_secret = auth.getTokenSecret();
           System.out.println(access_key);
           System.out.println(access_secret);

           DropboxAPI api = new DropboxAPI();
           DropboxAPI.Config conf = api.new Config(configuration);
           api.authenticateToken("XXXXXXXXXXXX", "XXXXXXXXXXX", conf);
           System.out.println(api.isAuthenticated());
           URL resource = this.getClass().getResource("/config/testing.json");
            
           File f= new File(resource.toURI());
           api.putFile("dropbox", "/Project", f);

           //api.accountInfo(); //even this method gives me a exception
        } catch (Exception e) {
                e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
            new DBmain();
        }
}

Exception

Exception in thread "main" java.lang.NoSuchMethodError: org.apache.http.protocol.BasicHttpContext: method <init>()V not found
    at org.apache.http.impl.client.DefaultHttpClient.createHttpContext(DefaultHttpClient.java:205)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:532)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
    at com.dropbox.client.DropboxClient.putFile(DropboxClient.java:299)
    at com.dropbox.client.DropboxAPI.putFile(DropboxAPI.java:463)
    at com.dropbox.client.DBmain.<init>(DBmain.java:58)
    at com.dropbox.client.DBmain.main(DBmain.java:70)
Java Result: 1

Some extra info

Edit Date:18-6-2011

I changed the httpclient-4.0-beta1.jar and httpcore-4.0-alpha6.jar to httpclient-4.0.jar and httpcore-4.0.1.jar respectively and I no longer get the above exception [ java.lang.NoSuchMethodError ] This stackoverflow question helped me in solving this : java.lang.NoSuchMethodError: org.apache.http.protocol.BasicHttpContext: method <init>()V not found

But Now I get UnknownhostException on execution of any methods of the API

com.dropbox.client.DropboxException: java.net.UnknownHostException: api.getdropbox.com:80
    at com.dropbox.client.RESTUtility.request(RESTUtility.java:250)
    at dump.DropboxClient.accountInfo(DropboxClient.java:121)
    at com.charan.client.DBmain.<init>(DBmain.java:57)
    at com.charan.client.DBmain.main(DBmain.java:65)
Caused by: java.net.UnknownHostException: api.getdropbox.com:80
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
    at java.net.Socket.connect(Socket.java:525)
    at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:123)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:123)
    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:147)
    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:101)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:381)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:641)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:576)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:554)
    at com.dropbox.client.RESTUtility.executeRequest(RESTUtility.java:172)
    at com.dropbox.client.RESTUtility.request(RESTUtility.java:248)
    ... 3 more
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should use http-client 4.0.3 jar


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...