我正在尝试从我的应用程序上传视频文件。
这是我到目前为止所得到的:
public class Download extends Application {
public interface upload {
@Multipart
@POST("new")
Call<Response> send(@Part("myFile") RequestBody file);
}
public void uploadFile(File xfile) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.0.3")
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestBody file = RequestBody.create(MediaType.parse("video/*"), xfile);
upload xUpload = retrofit.create(upload.class);
Call<Response> call = xUpload.send(file);
try {
Response result = call.execute().body();
}
catch (IOException e)
{
Log.d("TEST3", " didn't work ");
}
}
}我得到了下面的错误:‘report2.Response’不是一个有效的响应体类型。你是说ResponseBody吗?for method upload.send any ideas
我已经阅读了retrofit2的网页,并尝试了他们上传文件的主要示例,但由于两个原因,它不起作用。1.我找不到正确的ServiceGenerator 2.我的文件是在画廊中找到的,我将其内容流式传输到要上传的临时文件中,无法直接从其URI访问它...或者,我可以使用retrofit2吗?
发布于 2017-01-28 14:45:22
我用来上传图像从翻新2这样,它工作正常
File file = new File(image.getPath());
RequestBody mFile = RequestBody.create(MediaType.parse("image/*"), file);
MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("gallery", file.getName(), mFile);
RequestBody filename = RequestBody.create(MediaType.parse("text/plain"), id);
final NetworkCall networkCall=new NetworkCall(this);
Call<ResponseBody> call = networkCall.getRetrofit(false).uploadImage( filename, fileToUpload);
call.clone().enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});下面是我的网络调用类:
public class NetworkCall {
Context context;
ProgressDialog progressDialog;
public NetworkCall(Context context){
this.context = context;
}
public IApi getRetrofit(boolean isShowLoading){
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.connectTimeout(0, TimeUnit.SECONDS).readTimeout(0,TimeUnit.SECONDS);
httpClient.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
Request request = original.newBuilder()
.header("Content_type","application/json")
.header("Accept", "application/json")
.method(original.method(), original.body())
.build();
return chain.proceed(request);
}
});
Gson gson = new GsonBuilder()
.setLenient()
.create();
OkHttpClient client = httpClient.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
if (isShowLoading&&context instanceof BaseActivity)
showLoading();
// prepare call in Retrofit 2.0
IApi api = retrofit.create(IApi.class);
// Call<BaseResponce> call = api.callService(json);
//asynchronous call
// call.enqueue(this);
return api;
}
private void showLoading(){
try {
((BaseActivity)context).runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Please wait...");
progressDialog.setCancelable(false);
progressDialog.show();
}
});
}catch (Exception e){
e.printStackTrace();
}
}
public void dismissLoading(){
try {
((BaseActivity)context).runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog.cancel();
progressDialog.dismiss();
}
});
}catch (Exception e){
e.printStackTrace();
}
}
}我在IApi类中使用它
@Multipart
@POST("events/file_upload.json")
Call <ResponseBody> uploadImage(@Part("event_id") RequestBody id,@Part MultipartBody.Part part);希望能有所帮助
https://stackoverflow.com/questions/41905282
复制相似问题