Java 使用json API和android上传文件
嗨,我正在为bayfiles开发一个android应用程序,在那里你可以查看、删除和上传文件。他们有一个API,可以使用Json API将文件上传到服务器,但我知道怎么做。文件内容如下: /文件/上传URL {错误:“”,上载URL:,progressUrl:} 返回的URL在有限的时间内有效,您需要 为每次上传请求一个新的。向用户发出POST请求 “uploadUrl”以上载文件。您可以轮询“progressUrl”以获取 目前的进展。POST请求需要是多部分/表单数据 编码并包含一个“文件”字段。cUrl示例:cUrl-F “file=@somefile.rar” "" 注意:要将文件上载到您的帐户,您需要首先登录 使用/account/login,然后将?session=XYZ附加到 /文件/uploadUrl成功后,“uploadUrl”将以 对象:{错误:,文件ID:,大小:,sha1:, infoToken:,deleteToken:,linksUrl:, 下载URL:,删除URL:,}Java 使用json API和android上传文件,java,android,json,file-upload,curl,Java,Android,Json,File Upload,Curl,嗨,我正在为bayfiles开发一个android应用程序,在那里你可以查看、删除和上传文件。他们有一个API,可以使用Json API将文件上传到服务器,但我知道怎么做。文件内容如下: /文件/上传URL {错误:“”,上载URL:,progressUrl:} 返回的URL在有限的时间内有效,您需要 为每次上传请求一个新的。向用户发出POST请求 “uploadUrl”以上载文件。您可以轮询“progressUrl”以获取 目前的进展。POST请求需要是多部分/表单数据 编码并包含一个“文件”
我无法理解如何使用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( );
}