上传音频文件时,API 在邮递员中工作,但在 android 应用程序中不工作

Posted

技术标签:

【中文标题】上传音频文件时,API 在邮递员中工作,但在 android 应用程序中不工作【英文标题】:API work in postman but not in android app when upload audio file 【发布时间】:2020-01-28 01:01:07 【问题描述】:

**在 android 应用程序 API 中不起作用。在应用程序中,当我单击呼叫按钮时,呼叫将被记录并使用改造发送到服务器。但我检查邮递员 API 是否完全正常工作。我该怎么办? **

public class ApiClient 

public static final String BASE_URL = "BASE URL";

private static Retrofit retrofit = null;


public static Retrofit getClient() 

    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

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

    if (retrofit==null) 
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
    
    return retrofit;

**接口**

 //upload recorder file
@Multipart
@Headers("Content-Type:application/json",
        "ENCTYPE:multipart/form-data",
        "cache-control: no-cache"
)
@POST ("UploadToServer")
Call<UploadDataAudio> uploadRecordingFile(@Part MultipartBody.Part AudioComment);

从 MainActivity 调用 api

public void updateRecordingFileName(String idoc_id, String filename) 
    // Showing progress dialog
        Log.e("Audiosavefilename ->",""+filename);

    File file=new File(filename);
    Log.e("Audio path",""+file.getAbsolutePath());
    Log.e("audio size",""+file.getAbsoluteFile().lastModified());


    String mimeType= URLConnection.guessContentTypeFromName(file.getName());



    RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data") , file);
    MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("uploaded_file" , file.toString() , requestBody);


    final ProgressDialog pDialog = new ProgressDialog(MainActivity.this);
    pDialog.setMessage("Please wait...");
    pDialog.setCancelable(false);
    pDialog.show();

    APIInterface apiService = ApiClient.getClient().create(APIInterface.class);
    Call<UploadDataAudio> call = apiService.uploadRecordingFile(fileToUpload);
   Log.e("Audio url",""+ call.request().url().toString());
    call.enqueue(new Callback<UploadDataAudio>() 
        @Override
        public void onResponse(Call<UploadDataAudio> call, Response<UploadDataAudio> response) 
            Log.i("RETROFIT", "onResponse Called" + response.code());
            UploadDataAudio dataaudio=response.body();
            Log.e("RESPONSE",""+dataaudio.getResponseData().toString());
            Log.i("response",new Gson().toJson(dataaudio));
            if (pDialog.isShowing()) 
                pDialog.dismiss();
            

        

        @Override
        public void onFailure(Call<UploadDataAudio> call, Throwable t) 
            // Log error here since request failed
            if (pDialog.isShowing()) 
                pDialog.dismiss();
            
        
    );

**在邮递员中完成在这里工作 **

**当我上传音频文件时。此消息在调试模式下显示**

java.net.ProtocolException:预期 3286 字节,但收到 3699

【问题讨论】:

你收到响应了吗:Log.i("RETROFIT", "onResponse Called" + response.code()); 你能修复它吗?我遇到了类似的问题? 【参考方案1】:

您是否尝试过更改读写超时值? 更改超时,如果不起作用,请查看:-

public class RetrofitClient 
    static int cacheSize = 10 * 1024 * 1024; // 10 MB
    static Cache cache = new Cache(getCacheDir(), cacheSize);
    private static Retrofit retrofit = null;

    public static Retrofit getClient(String baseUrl) 
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(interceptor)
                .connectTimeout(200, TimeUnit.SECONDS)
                .readTimeout(200,TimeUnit.SECONDS)
                .build();
        if (retrofit == null) 

            retrofit = new Retrofit.Builder()
                    .baseUrl(baseUrl).client(okHttpClient)
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        
        return retrofit;
    

【讨论】:

以上是关于上传音频文件时,API 在邮递员中工作,但在 android 应用程序中不工作的主要内容,如果未能解决你的问题,请参考以下文章

如何解决 Http 401 错误,但在邮递员中工作,而不是在 xamarin 表单中工作

Django Rest Framework - response.set_cookie() 不在浏览器中设置 cookie,但在邮递员和可浏览 api 中工作

尽管在邮递员中工作,但请求 JSON 在 JMeter 中给出错误

Web Api 2.0 在邮递员中工作但不在 HTML 页面中

出现 405 错误-在邮递员中工作时方法不允许招摇撞错

cookie 中的访问令牌,但验证无法在 react.js(axios) 中检测到它,在邮递员中工作