gpt4 book ai didi

android - 使用 Retrofit 2 上传视频

转载 作者:行者123 更新时间:2023-11-29 17:29:04 25 4
gpt4 key购买 nike

我想使用 Retrofit 2 上传视频文件,请帮我做这件事。

这是我的端点:

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);
}
?>


我尝试在网络上遵循一个示例(将其与从库中选择的文件结合起来),但它甚至没有编译(我用注释标记了错误行):

主 Activity :

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:

public interface UploadApiService 
{
@Multipart
@POST("api/upload.php")
Call<ResponseBody> uploadVideo(@Part("description") String description, @Part("video") RequestBody video);
}

我想我必须使用一种 Stream 来跟踪进度,但不幸的是我不知道更多。

最佳答案

确保您导入的内容是正确的。例如,Callback 应该是 retrofit.Callback;,而不是 import com.squareup.okhttp.Callback;。根据您的评论,听起来真正的问题是与本地定义的 ResponseBody 的类型冲突。

关于android - 使用 Retrofit 2 上传视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33710504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com