title: Using Dropbox Java API for uploading files to dropbox
body: <p>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.<br />
  <code>Java Class</code></p>
  <pre><code>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(&quot;java.net.useSystemProxies&quot;, &quot;true&quot;);
          Map configuration= new HashMap();
          configuration.put(&quot;consumer_key&quot;, &quot;XXXXXXXXXXXXXXXX&quot;);
          configuration.put(&quot;consumer_secret&quot;, &quot;XXXXXXXXXXXXXXXX&quot;);
          configuration.put(&quot;request_token_url&quot;, &quot;http://api.dropbox.com/0/oauth/request_token&quot;);
          configuration.put(&quot;access_token_url&quot;, &quot;http://api.dropbox.com/0/oauth/access_token&quot;);
          configuration.put(&quot;authorization_url&quot;, &quot;http://api.dropbox.com/0/oauth/authorize&quot;);
          configuration.put(&quot;port&quot;,80);
          //configuration.put(&quot;trusted_access_token_url&quot;,&quot;http://api.getdropbox.com/0/token&quot;);
          configuration.put(&quot;server&quot;,&quot;api.getdropbox.com&quot;);
          configuration.put(&quot;content_server&quot;,&quot;api-content.dropbox.com&quot;);
          String username=&quot;myMailAddress@domain.com&quot;;
          String password=&quot;myPassword&quot;;
          try {
             Authenticator auth = new Authenticator(configuration);
             String url = auth.retrieveRequestToken(&quot;&quot;);
             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(&quot;XXXXXXXXXXXX&quot;, &quot;XXXXXXXXXXX&quot;, conf);
             System.out.println(api.isAuthenticated());
             URL resource = this.getClass().getResource(&quot;/config/testing.json&quot;);
              
             File f= new File(resource.toURI());
             api.putFile(&quot;dropbox&quot;, &quot;/Project&quot;, f);
  
             //api.accountInfo(); //even this method gives me a exception
          } catch (Exception e) {
                  e.printStackTrace();
          }
      }
      
      public static void main(String[] args) {
              new DBmain();
          }
  }
  </code></pre>
  <p><code>Exception</code></p>
  <pre><code>Exception in thread &quot;main&quot; java.lang.NoSuchMethodError: org.apache.http.protocol.BasicHttpContext: method &lt;init&gt;()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.&lt;init&gt;(DBmain.java:58)
      at com.dropbox.client.DBmain.main(DBmain.java:70)
  Java Result: 1
  </code></pre>
  <p><code>Some extra info</code>
  <a href="http://i296.photobucket.com/albums/mm176/charanraj1191g/dropbox.png" rel="nofollow noreferrer" title="Click to open the image"><img src="https://i.stack.imgur.com/ALMxR.png" alt="" /></a></p>
  
  <h2>Edit Date:18-6-2011</h2>
  <p>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 <code>[ java.lang.NoSuchMethodError ]</code>
  This stackoverflow question helped me in solving this : <a href="https://stackoverflow.com/questions/1617042/java-lang-nosuchmethoderror-org-apache-http-protocol-basichttpcontext-method-i">java.lang.NoSuchMethodError: org.apache.http.protocol.BasicHttpContext: method &lt;init&gt;()V not found</a></p>
  <p>But Now I get UnknownhostException on execution of any methods of the API</p>
  <pre><code>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.&lt;init&gt;(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
  </code></pre>