Android 更新2上传文件
我正在尝试从我的应用程序上载视频文件 以下是到目前为止我得到的信息:Android 更新2上传文件,android,retrofit2,Android,Retrofit2,我正在尝试从我的应用程序上载视频文件 以下是到目前为止我得到的信息: public class Download extends Application { public interface upload { @Multipart @POST("new") Call<Response> send(@Part("myFile") RequestBody file); } public void uploadFile(File xfile) { Re
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 ");
}
}
}
公共类下载扩展了应用程序{
公共接口上传{
@多部分
@职位(“新”)
调用send(@Part(“myFile”)RequestBody文件);
}
公共无效上载文件(文件xfile){
改装改装=新改装.Builder()
.baseUrl(“http://192.168.0.3")
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestBody file=RequestBody.create(MediaType.parse(“video/*”),xfile);
upload xUpload=reformation.create(upload.class);
Call Call=xUpload.send(文件);
试一试{
响应结果=call.execute().body();
}
捕获(IOE异常)
{
Log.d(“TEST3”,“不起作用”);
}
}
}
我收到以下错误消息2。响应“”不是有效的响应正文类型。你的意思是回应书吗?方法上传。发送任何想法
我已经阅读了Reformation2的网页,并尝试了他们上传文件的主要示例,但由于两个原因,它不起作用。
1.我找不到正确的ServiceGenerator
2.我的文件是在图库中找到的,我将其内容流式传输到一个临时文件中,我将上载该文件,我无法直接从其URI访问它。。。或者我可以使用改型2吗?我用这种方式上传改型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) {
}
});
File File=新文件(image.getPath());
RequestBody mFile=RequestBody.create(MediaType.parse(“image/*”),文件);
MultipartBody.Part fileToUpload=MultipartBody.Part.createFormData(“gallery”,file.getName(),mFile);
RequestBody filename=RequestBody.create(MediaType.parse(“text/plain”),id);
最终NetworkCall NetworkCall=新的NetworkCall(此);
Call Call=networkCall.getRefundation(false).uploadImage(文件名,fileToUpload);
call.clone().enqueue(新回调()命令){
@凌驾
公共void onResponse(调用、响应){
}
@凌驾
失败时公共无效(调用调用,可丢弃的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();
}
}
}
公共类网络调用{
语境;
进行对话进行对话;
公共网络呼叫(上下文){
this.context=上下文;
}
公共IApi GetReformation(布尔ISSOWLOAding){
OkHttpClient.Builder httpClient=新建OkHttpClient.Builder();
httpClient.connectTimeout(0,TimeUnit.SECONDS)。readTimeout(0,TimeUnit.SECONDS);
httpClient.addInterceptor(新的拦截器(){
@凌驾
公共响应拦截(链)引发IOException{
Request original=chain.Request();
请求=original.newBuilder()
.header(“内容类型”、“应用程序/json”)
.header(“接受”、“应用程序/json”)
.method(original.method(),original.body())
.build();
返回链。继续(请求);
}
});
Gson Gson=new GsonBuilder()
.setLenient()
.create();
OkHttpClient=httpClient.build();
改装改装=新改装.Builder()
.baseUrl(常量.BASE\u URL)
.客户(客户)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
if(isShowLoading&&BaseActivity的上下文实例)
showLoading();
//准备呼叫改装2.0
iapiapi=reformation.create(IApi.class);
//Call=api.callService(json);
//异步调用
//call.enqueue(这个);
返回api;
}
私有void showLoading(){
试一试{
((BaseActivity)context).runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
progressDialog=新建progressDialog(上下文);
progressDialog.setMessage(“请稍候…”);
progressDialog.setCancelable(假);
progressDialog.show();
}
});
}捕获(例外e){
e、 printStackTrace();
}
}
公共无效解除加载(){
试一试{
((BaseActivity)context).runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
progressDialog.cancel();
progressDialog.disclose();
}
});
}捕获(例外e){
e、 printStackTrace();
}
}
}
我在IApi课上用这个
@Multipart
@POST("events/file_upload.json")
Call <ResponseBody> uploadImage(@Part("event_id") RequestBody id,@Part MultipartBody.Part part);
@Multipart
@POST(“事件/文件_upload.json”)
调用uploadImage(@Part(“event_id”)RequestBody id,@Part MultipartBody.Part);
希望它能有所帮助我最近已经按照本教程完成了文件上传