问题描述
我想使用 Retrofit 2 上传视频文件,请帮我完成.
I want to upload a video file using Retrofit 2, please help me doing this.
这是我的端点:
upload.php
:
upload.php
:
<?php
$uploaddir = '../uploads/';
$uploadfile = $_FILES['userfile']['name'];
$response = array();
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir.$uploadfile))
{
$response["result"] = "1";
echo json_encode($response);
} else
{
$response["result"] = "-1";
echo json_encode($response);
}
?>
我试图在网上跟踪一个示例(将它与图库中的文件选择相结合),但它甚至没有编译(我已经用评论标记了错误的行):
I tried to follow a sample (combining it with file selection from the gallery) on the web, but it doesn't even compile (I have marked the erroneous line by a comment):
MainActivity
:
MainActivity
:
public class MainActivity extends Activity
{
private static int RESULT_LOAD_VIDEO = 1;
String decodableString;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btn_load = (Button) findViewById(R.id.buttonLoadVideo);
btn_load.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
loadVideoFromGallery(btn_load);
}
});
}
/*
* PICK THE VIDEO AND EXTRACT ITS ADDRESS
*/
public void loadVideoFromGallery(View view)
{
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_VIDEO);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
try {
// When a video is picked
if (requestCode == RESULT_LOAD_VIDEO && resultCode == RESULT_OK
&& null != data)
{
// Get the video from data
Uri selectedVideo = data.getData();
String[] filePathColumn = { MediaStore.Video.Media.DATA };
Cursor cursor = getContentResolver().query(selectedVideo,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
decodableString = cursor.getString(columnIndex);
Log.i("mok",decodableString);
cursor.close();
upload(decodableString);
} else
{
Toast.makeText(this, "You haven't picked any video",
Toast.LENGTH_LONG).show();
}
} catch (Exception e)
{
e.printStackTrace();
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}
}
/*
* UPLOAD THE SELECTED VIDEO TO THE SRVER
*/
public void upload(String decodableString)
{
final String BASE_URL = "http://192.168.1.7/";
Retrofit retrofit = new Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
UploadApiService service = retrofit.create(UploadApiService.class);
MediaType MEDIA_TYPE = MediaType.parse("video/mp4");
File file = new File("/storage/emulated/0/Pictures/MyApp/test.png");
RequestBody requestBody = RequestBody.create(MEDIA_TYPE, file);
Call<com.squareup.okhttp.ResponseBody> call = service.uploadVideo("desc", requestBody);
call.enqueue(new Callback<ResponseBody>(){ //error even before compile***
@Override
public void onResponse(Response<ResponseBody> response, Retrofit retrofit)
{
// TODO Auto-generated method stub
if (response.isSuccess())
{
Log.i("mok","S");
ResponseBody rb = response.body();
Log.i("mok",rb.getUsername());
}
else
{
Log.i("mok","F");
com.squareup.okhttp.ResponseBody rb = response.errorBody();
}
}
@Override
public void onFailure(Throwable t)
{
t.printStackTrace();
Log.i("mok",t.getCause()+"");
Log.i("mok","T");
finish();
}
});
}
}
UploadApiService
:
UploadApiService
:
public interface UploadApiService
{
@Multipart
@POST("api/upload.php")
Call<ResponseBody> uploadVideo(@Part("description") String description, @Part("video") RequestBody video);
}
我想我必须使用一种 Stream
来跟踪进度,但不幸的是我不知道更多.
I guess I have to use a kind of Stream
to keep track of the progress but unfortunately I don't know more.
推荐答案
确保您的导入是正确的.例如,Callback
应该是 retrofit.Callback;
,而不是 import com.squareup.okhttp.Callback;
.根据您的评论,听起来真正的问题是与本地定义的 ResponseBody
的类型冲突.
Make sure you imports are the correct. For example, Callback
should be retrofit.Callback;
, not import com.squareup.okhttp.Callback;
. Based on your comment, it sounds like the real problem was a type conflict with a locally defined ResponseBody
.
这篇关于使用 Retrofit 2 上传视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!