我一直在学习各种教程,并试图使用android中的改装将视频上传到我的服务器。我需要的唯一参数如下图所示

我继续得到超时异常,即使我已经增加了超时。这是我上传的视频代码。
final OkHttpClient okHttpClient = new OkHttpClient.Builder()
.readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS)
.build();
Log.v("test_get", "get the file");
//MultipartBody.Part vFile = MultipartBody.Part.createFormData("video", videoFile.getName(), videoBody);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://xxxx:xxx/")
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
SmileVideoAPI service = retrofit.create(SmileVideoAPI.class);
MediaType MEDIA_TYPE = MediaType.parse("video/mp4");
File videoFile = new File(pathToVideoFile);
RequestBody videoBody = RequestBody.create(MEDIA_TYPE, videoFile);
Log.v("test_get", "before uploading");
Call<ResponseBody> call = service.uploadVideo("desc", videoBody);
Log.v("test_get", "after uploading");
call.enqueue(new Callback<ResponseBody>(){
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful())
{
Log.i("mok","S");
ResponseBody rb = response.body();
Log.i("mok",rb.toString());
}
else {
Log.i("mok", "F");
ResponseBody rb = response.errorBody();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
t.printStackTrace();
Log.i("mok",t.getCause()+"");
Log.i("mok","T");
finish();
}
});
return msg;
I have been trying taking reference from this post : 发布于 2017-02-21 04:50:16
如果您希望上传文件(任何类型),这里有一种适用于Android的PHP方法,效果很好。
您需要服务器上的一个脚本来接收文件:
uploadfile.php
<?php
$fname = $_POST['filename'];
$target_path = "/yourserverpath/".$fname;
$upload_path = $_FILES['uploadedfile']['tmp_name'];
If (move_uploaded_file($upload_path, $target_path)) {
echo "Moved";
} else {
echo "Not Moved";
}
?>您可以在服务器上使用以下uploadfile.php测试器文件在浏览器上测试您的HTML:
Uploadfile.html:
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload:
<input name="uploadedfile" type="file" /><br />
Filename on server:
<input name="filename" type="text" /><br />
<br />
<input type="submit" value="Upload File" />
</form>一旦服务器端开始从浏览器上传文件,那么Android部分就非常简单了:
上传器处理文件的上传。该类调用服务器上的uploadfile.php脚本。
上传文件可能很棘手,但我们有一个秘密武器,使它更容易。我们将使用DefaultHttpClient,它允许我们访问MultipartEntity方法。当使用HttpUrlConnection时,此方法不可用。fileUpload类放置在AsyncTask中。我的示例中的selectedPicName只是您的文件名。postURL是前面提到的脚本的完整URL。
private class fileUpload extends AsyncTask<Void, String, Void> {
protected Void doInBackground(Void... unused) {
// upload new picture to the server
String postURL = uploadFilesScript;
File file = new File(path2, selectedPicName);
// upload the new picture
Uploader(postURL, file, selectedPicName);
}
}Uploader类如下所示(请注意,“上载文件”名称必须与接收上传的PHP脚本中的变量匹配。)
public static void Uploader(String postURL, File file, String fname) {
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost(postURL);
// the boundary key below is arbitrary,
// it just needs to match the MPE so it can decode multipart correctly
httppost.setHeader("Content-Type", "multipart/form-data; boundary=--32530126183148");
MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,
"--32530126183148",
Charset.forName("UTF-8"));
mpEntity.addPart("uploadedfile", new FileBody((file), "application/txt"));
mpEntity.addPart("MAX_FILE_SIZE", new StringBody("100000"));
mpEntity.addPart("filename", new StringBody(fname));
}
httppost.setEntity(mpEntity);
HttpResponse response;
response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
resEntity.consumeContent();
Log.v("UPLOAD", "resEntity=" + resEntity.toString());
}
httpclient.getConnectionManager().shutdown();
} catch (Throwable e) {
}
}MultipartEntity允许我们使用addPart方法简单地指定文件名和文件内容。在下面的示例中,变量名与服务器上接收PHP脚本所期望的名称匹配。
如果我们必须使用HttpUrlConnection类来完成上传功能,这将非常复杂,因为我们必须创建自己的文件处理包装器。MultipartEntity还允许我们指定字符集和最大文件大小。需要提供唯一但任意的边界字符串,该字符串必须与服务器脚本匹配。
我一直在我的生产应用程序中使用这种方法,而且效果很好。
充分披露-我写了一本书,名为安卓软件开发:一个实用项目的集合和上传在服务器旋转应用程序在第5章,但这应该是你所需要的上传视频。
https://stackoverflow.com/questions/42358548
复制相似问题