2013-11-21 46 views
0

我想上傳一個文本文件到Dropbox。身份驗證工作正常,但當它嘗試上載文件時,應用程序崩潰。這裏是我的代碼將文件上傳到Dropbox從android應用程序

public class Dropboxupload extends Activity { 
    final static private String APP_KEY = "KEY"; 
    final static private String APP_SECRET = "SECRET"; 
    final static private AccessType ACCESS_TYPE = AccessType.APP_FOLDER; 
    private DropboxAPI<AndroidAuthSession> mDBApi; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main); 
     AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET); 
     AndroidAuthSession session = new AndroidAuthSession(appKeys, ACCESS_TYPE); 
     mDBApi = new DropboxAPI<AndroidAuthSession>(session); 
     mDBApi.getSession().startAuthentication(Dropboxupload.this); 


    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.dropboxupload, menu); 
     return true; 
    } 

    /* Called when the application resumes */ 
    @Override 
    protected void onResume() 
    { 
     super.onResume(); 

     if (mDBApi.getSession().authenticationSuccessful()) { 
      try { 
       // Required to complete auth, sets the access token on the session 
       mDBApi.getSession().finishAuthentication(); 

       AccessTokenPair tokens = mDBApi.getSession().getAccessTokenPair(); 
      } catch (IllegalStateException e) { 
       Log.i("DbAuthLog", "Error authenticating", e); 
      } 

      String filePath = getApplicationContext().getFilesDir().getPath().toString() + "/magnus-opus.txt"; 

      File file = new File(filePath); 


      try { 
       file.createNewFile(); 
      } catch (IOException e2) { 
       // TODO Auto-generated catch block 
       e2.printStackTrace(); 
      } 



      FileInputStream inputStream = null; 

      try { 
       inputStream = new FileInputStream(file); 
      } catch (FileNotFoundException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
      try { 
       com.dropbox.client2.DropboxAPI.Entry response = mDBApi.putFile("/magnum-opus.txt", inputStream, 
         file.length(), null, null); 
       Log.i("DbExampleLog", "The uploaded file's rev is: " + response.rev); 
      } catch (DropboxException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 
    } 


private void writeToFile(String data,String filepath, String filename) { 
     try { 
      OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput(filepath, Context.MODE_PRIVATE)); 
      outputStreamWriter.write(data); 
      outputStreamWriter.close(); 
     } 
     catch (IOException e) { 
      Log.e("Exception", "File write failed: " + e.toString()); 
     } 
    } 
} 

我也在這裏發佈錯誤日誌,任何幫助,將不勝感激。

11-21 18:04:20.094: W/System.err(16838): DropboxServerException (nginx): 403 Forbidden (Forbidden) 
11-21 18:04:20.094: W/System.err(16838): at com.dropbox.client2.RESTUtility.parseAsJSON(RESTUtility.java:263) 
11-21 18:04:20.094: W/System.err(16838): at com.dropbox.client2.RESTUtility.execute(RESTUtility.java:411) 
11-21 18:04:20.094: W/System.err(16838): at com.dropbox.client2.DropboxAPI$BasicUploadRequest.upload(DropboxAPI.java:1080) 
11-21 18:04:20.104: W/System.err(16838): at com.dropbox.client2.DropboxAPI.putFile(DropboxAPI.java:1421) 
11-21 18:04:20.104: W/System.err(16838): at com.example.screenwritter.Dropboxupload$1.run(Dropboxupload.java:85) 
11-21 18:04:20.104: W/System.err(16838): at java.lang.Thread.run(Thread.java:856) 
11-21 18:04:43.506: W/IdleConnectionHandler(16838): Removing a connection that never existed! 
+1

Dropbox拒絕你訪問,但你的代碼看起來沒問題。你是否正確地遵循了他們的設置程序 –

+0

是的一切。不幸的是它不工作 –

回答

2

您正在嘗試在主線程上執行網絡操作,該線程被禁止以避免阻塞UI。詳情請參閱the documentation of NetworkOnMainThreadException

+0

我試圖在其他線程運行它,仍然不工作 –

+0

什麼是「不工作」是什麼意思?請將新的錯誤添加到問題中。 –

+0

我已經添加了新的日誌,沒有錯誤,但有一些警告導致它停止上傳過程 –

1

的問題是線

final static private String APP_KEY = "KEY"; 
final static private String APP_SECRET = "SECRET"; 

您應填寫由Dropbox的提供的值。

相關問題