2013-11-21 2 views
0

텍스트 파일을 드롭 박스에 업로드하려고합니다. 인증 잘 작동하지만 파일을 업로드 할 때 응용 프로그램이 충돌합니다. 여기 내 코드안드로이드 응용 프로그램에서 파일을 드롭 상자에 업로드

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"; 

당신은 작성해야합니다.

관련 문제