0
A
回答
1
以下代码是将视频上传到服务器的后台任务示例。没有什么特定的Android Studio - 无论您的IDE是Eclipse还是Studio(或其他任何东西),它都应该可以工作。
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.os.AsyncTask;
import android.util.Log;
public class VideoUploadTask extends AsyncTask<String, String, Integer> {
/* This Class is an AsynchTask to upload a video to a server on a background thread
*
*/
private VideoUploadTaskListener thisTaskListener;
private String serverURL;
private String videoPath;
public VideoUploadTask(VideoUploadTaskListener ourListener) {
//Constructor
Log.d("VideoUploadTask","constructor");
//Set the listener
thisTaskListener = ourListener;
}
@Override
protected Integer doInBackground(String... params) {
//Upload the video in the background
Log.d("VideoUploadTask","doInBackground");
//Get the Server URL and the local video path from the parameters
if (params.length == 2) {
serverURL = params[0];
videoPath = params[1];
} else {
//One or all of the params are not present - log an error and return
Log.d("VideoUploadTask doInBackground","One or all of the params are not present");
return -1;
}
//Create a new Multipart HTTP request to upload the video
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(serverURL);
//Create a Multipart entity and add the parts to it
try {
Log.d("VideoUploadTask doInBackground","Building the request for file: " + videoPath);
FileBody filebodyVideo = new FileBody(new File(videoPath));
StringBody title = new StringBody("Filename:" + videoPath);
StringBody description = new StringBody("Test Video");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("videoFile", filebodyVideo);
reqEntity.addPart("title", title);
reqEntity.addPart("description", description);
httppost.setEntity(reqEntity);
} catch (UnsupportedEncodingException e1) {
//Log the error
Log.d("VideoUploadTask doInBackground","UnsupportedEncodingException error when setting StringBody for title or description");
e1.printStackTrace();
return -1;
}
//Send the request to the server
HttpResponse serverResponse = null;
try {
Log.d("VideoUploadTask doInBackground","Sending the Request");
serverResponse = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
//Log the error
Log.d("VideoUploadTask doInBackground","ClientProtocolException");
e.printStackTrace();
} catch (IOException e) {
//Log the error
Log.d("VideoUploadTask doInBackground","IOException");
e.printStackTrace();
}
//Check the response code
Log.d("VideoUploadTask doInBackground","Checking the response code");
if (serverResponse != null) {
Log.d("VideoUploadTask doInBackground","ServerRespone" + serverResponse.getStatusLine());
HttpEntity responseEntity = serverResponse.getEntity();
if (responseEntity != null) {
//log the response code and consume the content
Log.d("VideoUploadTask doInBackground","responseEntity is not null");
try {
responseEntity.consumeContent();
} catch (IOException e) {
//Log the (further...) error...
Log.d("VideoUploadTask doInBackground","IOexception consuming content");
e.printStackTrace();
}
}
} else {
//Log that response code was null
Log.d("VideoUploadTask doInBackground","serverResponse = null");
return -1;
}
//Shut down the connection manager
httpclient.getConnectionManager().shutdown();
return 1;
}
@Override
protected void onPostExecute(Integer result) {
//Check the return code and update the listener
Log.d("VideoUploadTask onPostExecute","updating listener after execution");
thisTaskListener.onUploadFinished(result);
}
}
相关问题
- 1. Android视频上传到Php服务器?
- 2. 从服务器上传视频到Facebook
- 3. 上传视频文件到服务器
- 4. Swift上传视频到Http服务器?
- 5. 在android上使用volley将视频上传到服务器
- 6. android上传视频和字符串到服务器
- 7. 如何使用Android录制视频并上传到服务器?
- 8. 从android应用上传视频到http服务器
- 9. 如何上传视频或图像到服务器在android
- 10. 在服务器上传视频
- 11. Vimeo的视频上传视频上传到视频居然被上传服务器
- 12. 将视频上传到网络服务
- 13. 从android设备上传视频并在服务器上发布该视频
- 14. 录制视频和音频并上传到服务器
- 15. 上传图像/音频/视频到iphone服务器..?
- 16. 将视频从java上传到php服务器时出错
- 17. 网络服务器到Android视频流
- 18. Android - 视频流到服务器
- 19. 服务器广播RTSP视频到Android
- 20. 播放框架2.2.1视频上传到服务器上的CloudBees
- 21. iOS上的视频上传到服务器
- 22. 钛加速器上传录制的视频到服务器
- 23. 视频服务器上
- 24. 视频上传服务器问题在android
- 25. 上传大视频从Android PHP服务器崩溃应用
- 26. 如何上传视频android文件zilla服务器
- 27. Android视频上传到Youtube
- 28. 从Android手机到服务器的视频文件传输
- 29. 流视频到服务器
- 30. 不能上传大于500kb的视频文件大小到Android服务器中的Android服务器
这可以通过loopj android-async-http库完成吗? –
我个人没有用过,但没有理由不。如果你今天选择了一个HTTP库,那么查看Volley和Retrofit也许是有意义的。 – Mick