使用Retrofit2上传文件-正在上传文件,但响应“ onFailure”

时间:2018-07-13 13:03:20

标签: android file upload retrofit2

我需要将图像上传为文件,已经从堆栈中尝试了不同的解决方案,但仍然遇到问题。有人可以向我解释我在做什么错吗?

上传功能

private void EnviarFotos(){
    iGeoPBService geoPBService = GeoPBService.getInstance().usarServico();
    List<Foto> listaFotos = bancoDadosController.syncFotos();
    for (final Foto foto : listaFotos){
        String numObra = foto.getNumeroObra();
        String idAcomp = foto.getIdAcompanhamento();
        String idMedicao = foto.getIdMedicao();
        String imagem = foto.getImagem();
        final File file = FileFoto(imagem);
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"),file);
        MultipartBody.Part multipartBody = MultipartBody.Part.createFormData("file",foto.getNomeFoto(),requestFile);
        if (idAcomp.equals("N/A")){
            Call<String> envioMed = geoPBService.uploadFotoMedicao(idJurisdicionado,numObra,idMedicao,multipartBody);
            envioMed.enqueue(new Callback<String>() {
                @Override
                public void onResponse(Call<String> call, Response<String> response) {
                    if (response.isSuccessful()){
                        bancoDadosController.atualizaStatusSyncFoto(foto.getNomeFoto());
                        AttInfos();
                        Log.d("TESTE","FOTO ENVIADA COM SUCESSO!!!");
                        file.delete();
                    }else {
                        Log.d("TESTE","msg :"+ response.message());
                        Log.d("TESTE","errorBody :"+ response.errorBody().toString());
                        Toast.makeText(getBaseContext(),response.message(),Toast.LENGTH_SHORT).show();
                    }


                }

                @Override
                public void onFailure(Call<String> call, Throwable t) {
                    Log.d("TESTE","DEU MERDA!!!");
                    Log.d("TESTE","throw :"+ t.getMessage());
                    Log.d("TESTE","cause :"+ t.getCause());
                    Log.d("TESTE","lmsg :"+ t.getLocalizedMessage());
                    Log.d("TESTE","Stack :"+ t.getStackTrace().toString());

                    Toast.makeText(getBaseContext(),t.getMessage(),Toast.LENGTH_SHORT).show();

                }
            });

        }else {
            Call<String> envioAcomp = geoPBService.uploadFotoAcompanhamento(idJurisdicionado,numObra,idAcomp,multipartBody);
            envioAcomp.enqueue(new Callback<String>() {
                @Override
                public void onResponse(Call<String> call, Response<String> response) {
                    AttInfos();
                    Log.d("TESTE","FOTO ENVIADA COM SUCESSO!!!");
                    file.delete();

                }

                @Override
                public void onFailure(Call<String> call, Throwable t) {
                    Log.d("TESTE","DEU MERDA!!!");
                    Toast.makeText(getBaseContext(),t.getMessage(),Toast.LENGTH_SHORT).show();
                }
            });

        }

    }
}

界面

public interface iGeoPBService {

@Multipart
@POST("obras/uploadFotoMedicao/{jurisdicionado}/{numObra}/{idMedicao}")
Call<String> uploadFotoMedicao(@Path("jurisdicionado") String idJurisdicionado,@Path("numObra")String numObra,@Path("idMedicao") String idMedicao, @Part MultipartBody.Part file);

@Multipart
@POST("obras/uploadFotoAcompanhamento/{jurisdicionado}/{numObra}/{idAcompanhamento}")
Call<String> uploadFotoAcompanhamento(@Path("jurisdicionado") String idJurisdicionado, @Path("numObra")String numObra, @Path("idAcompanhamento") String idAcompanhamento, @Part MultipartBody.Part file);

}

我的代码已经根据其他答案进行了一些更改,这类似于关于使用Retrofit2上传文件的类似问题,但是执行我的代码后,代码始终落在“ onFailure”上

Logcat显示此内容:

07-13 14:14:44.035 8937-8937/despesalegal.tce.pb.gov.br.despesalegal D/TESTE: 
throw :Expected value at line 1 column 1 path $
07-13 14:14:44.035 8937-8937/despesalegal.tce.pb.gov.br.despesalegal D/TESTE: 
cause :null
07-13 14:14:44.035 8937-8937/despesalegal.tce.pb.gov.br.despesalegal D/TESTE: 
localized msg :Expected value at line 1 column 1 path $
07-13 14:14:44.035 8937-8937/despesalegal.tce.pb.gov.br.despesalegal D/TESTE: 
Stacktrace :[Ljava.lang.StackTraceElement;@b4b765b

我的文件已上传到服务器,但采用了改进的“ onFailure”响应方法

我做错了什么?实际上,问题可能出在服务器端?

[编辑]

我在代码上做了一些更改,现在我正确地获得了“ onResponse”。

服务等级

private void buildRetrofit(String baseUrl) {

    *OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
    clientBuilder.addInterceptor(logging);
    clientBuilder.retryOnConnectionFailure(false);
    clientBuilder.followRedirects(true);
    OkHttpClient client = clientBuilder.retryOnConnectionFailure(true).build();*

    *Gson gson = new GsonBuilder()
            .setLenient()
            .create();*

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(baseUrl)
            *.client(client)*
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();

    this.requestsGeo = retrofit.create(iGeoPBService.class);
}

界面

@Multipart
@POST("obras/uploadFotoMedicao/{jurisdicionado}/{numObra}/{idMedicao}")
Call<ResponseBody> uploadFotoMedicao(@Path("jurisdicionado") String idJurisdicionado, @Path("numObra")String numObra, @Path("idMedicao") String idMedicao, @Part MultipartBody.Part file, @Part("name") RequestBody name);

@Multipart
@POST("obras/uploadFotoAcompanhamento/{jurisdicionado}/{numObra}/{idAcompanhamento}")
Call<ResponseBody> uploadFotoAcompanhamento(@Path("jurisdicionado") String idJurisdicionado, @Path("numObra")String numObra, @Path("idAcompanhamento") String idAcompanhamento,@Part MultipartBody.Part body,@Part("name") RequestBody name);

1 个答案:

答案 0 :(得分:0)

只需将您的RequestBody更改为此:

type

您发送的MediaType错误...