2013-10-09 138 views
0

嗨我工作的android應用程序bayfiles,您可以在其中查看,刪除和上傳文件。他們有一個使用Json API將文件上傳到服務器的API,但是我可以弄清楚如何做到這一點。什麼文檔說:使用json API上傳文件android

/file/uploadUrl

{ error: "", uploadUrl: , progressUrl: }

The returned URL is valid for a limited amount of time, you need to request a new one for each upload. Issue a POST request to the "uploadUrl" to upload the file. You can poll "progressUrl" to fetch the current progress. The POST request needs to be multipart/form-data encoded and contain a "file" field. cUrl example: curl -F "[email protected]" " http://s6.baycdn.com/upload/93b37e57/4e691e0c/6/0?X-Progress-ID=4e639fcd34CC2F73796CF81679C605643A8F99CF " Note: in order to upload a file to your account, you need to first log in using /account/login and then append ?session=XYZ to /file/uploadUrl On success, the "uploadUrl" will respond with an object: { error: "", fileId: , size: , sha1: , infoToken: , deleteToken: , linksUrl: , downloadUrl: , deleteUrl: , }

我不能理解如何使用android做到這一點。任何幫助深表感謝!

回答

2

您可以使用此代碼上傳您的文件

private void uploadVideo(String yourfilepath) throws ParseException, IOException { 

       HttpClient httpclient = new DefaultHttpClient(); 

       //post request to send the video 
       HttpPost httppost = new HttpPost("your link to uploadd"); 
       StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
       StrictMode.setThreadPolicy(policy); 
       FileBody video_file1 = new FileBody(new File(videoPath)); 
       MultipartEntity reqEntity = new MultipartEntity(); 
       reqEntity.addPart("your argument that your server take", video_file1);      
       httppost.setEntity(reqEntity); 

       // DEBUG 
       System.out.println("executing request " + httppost.getRequestLine()); 
       HttpResponse response = httpclient.execute(httppost); 
       HttpEntity resEntity = response.getEntity(); 

       // DEBUG 
       System.out.println(response.getStatusLine()); 
       if (resEntity != null) { 
        System.out.println(EntityUtils.toString(resEntity)); 
       } // end if 
       if (resEntity != null) { 
        resEntity.consumeContent(); 
       } // end if 

       httpclient.getConnectionManager().shutdown(); 
      } 
+0

謝謝!這是正確的回答:) –