我想将带有一些数据的 mp3 文件从 android 上传到服务器。在这里,我需要同时发送一些带有该 mp3 文件的参数。我已经搜索了很多,但没有得到正确的结果。如果有人有任何想法,请与我分享示例。我确实是。
提前致谢。
使用此代码与其他参数一起上传。
private void uploadVideo(String audioPath,String param1,String param2) throws ParseException, IOException {
HttpClient httpclient = new DefaultHttpClient();
//post request to send the video
HttpPost httppost = new HttpPost("your url of server");
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy( policy);
FileBody audio_file1 = new FileBody(new File(audiopath));
StringBody param1 = new StringBody("your parameter1");
StringBody param21 = new StringBody("your parameter2");
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("argument that your server take", audio_file1);
reqEntity.addPart("argument that your server take", param1);
reqEntity.addPart("argument that your server take", param2);
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( );
}
在您的异步任务中使用此代码:
File yourfile= new File(your path on sdcard);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost(YOUR URL);
MultipartEntity entity = new MultipartEntity();
entity.addPart("FileName",new StringBody(yourfile));
entity.addPart("FileDescription",new StringBody(yourfileName));
httpost.setEntity(entity);
HttpResponse response = httpclient.execute(httpost);