gpt4 book ai didi

java - 使用json API上传文件android

转载 作者:行者123 更新时间:2023-12-02 06:39:44 25 4
gpt4 key购买 nike

您好,我正在开发一款用于 bayfiles 的 Android 应用程序,您可以在其中查看、删除和上传文件。他们有一个 API,可以使用 Json API 将文件上传到服务器,但我可以弄清楚如何执行此操作。文档内容如下:

/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 "file=@somefile.rar" "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 来做到这一点。非常感谢任何帮助!

最佳答案

您可以使用此代码上传您的文件

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( );
}

关于java - 使用json API上传文件android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19270881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com