使用改造的多部分请求无法连接到端点

时间:2018-12-18 12:00:45

标签: android retrofit2 okhttp

我正在尝试使用改造2将视频通过webAPI上传到服务器。 here is the link to the API

发布请求如下:

POST /videos/vitq4gOj8GyDT9kyxPQoyNJl/source HTTP/1.1
Host: ws.api.video
Authorization: Bearer access_token
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="source.mp4"
Content-Type: video/mp4


------WebKitFormBoundary7MA4YWxkTrZu0gW--

这是我的API接口:

public interface Client {
    @Multipart
    @POST("/videos/{videoId}/source")
    Call<ResponseBody> uploadVideo(@Path("videoId") String videoId,
                                   @Part MultipartBody.Part file);
    }

我在主要活动中的实际实现是这样的:

upload.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        //getting file and making a responce body
                        File video = new File(files.toString());
                        MultipartBody.Part file = MultipartBody.Part.createFormData("Title",video.getName(),RequestBody.create(MediaType.parse("video/*"),video));
                        //Inteceptor
                        OkHttpClient.Builder okhttpclient = new OkHttpClient.Builder();
                        okhttpclient.addInterceptor(new Interceptor() {
                            @Override
                            public Response intercept(Chain chain) throws IOException {
                                Request request = chain.request();
                                Request newRequests = request.newBuilder().
                                        addHeader("Host","ws.api.video").
                                        addHeader("Authorization",token_type+" "+accesTesxt.getText()).
                                        addHeader("Content-Type","multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW").
                                        build();

                                return chain.proceed(newRequests);
                            }
                        });
                        Retrofit.Builder builder = new Retrofit.Builder().
                                baseUrl("https://ws.api.video").
                                addConverterFactory(GsonConverterFactory.create()).
                                client(okhttpclient.build());
                        Retrofit retrofit1 = builder.build();
                        Client client1 = retrofit1.create(Client.class);

                        Call<ResponseBody> call1 = client1.uploadVideo(getVideoId.getText().toString(),file);
                        call1.enqueue(new Callback<ResponseBody>() {
                            @Override
                            public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
                                Toast.makeText(MainActivity.this,"Uploaded",Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onFailure(Call<ResponseBody> call, Throwable t) {
                                Toast.makeText(MainActivity.this,t.getMessage(),Toast.LENGTH_SHORT).show();
                            }
                        });



                    }
                });

当我单击实际上应该上传视频文件的按钮时,却从onFailure这样的方法中收到了祝酒消息:

Toast message by the <code>onFailure</code> method

谁能指出我在哪里做错

0 个答案:

没有答案