我正在尝试发送转换后的文件(Base64字符串)作为POST中的参数,文件大约有8MB,但发送大约需要4分钟。有没有加速的方法?
接口:
@FormUrlEncoded
@POST("upload")
Call<Upload> upload(@Field("CONTENT") String content);改装实例:
public class RetrofitClientInstance {
private static Retrofit retrofit;
private static OkHttpClient client;
public static Retrofit getRetrofitInstance(String url) {
if (retrofit == null && !url.isEmpty()) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
client = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.build();
retrofit = new retrofit2.Retrofit.Builder()
.baseUrl(url)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}}呼叫:
private void upload(){
Api api = RetrofitClientInstance.getRetrofitInstance(SharedUtils.SERVER_URL).create(Api.class);
Call<Upload> request = api.upload(getBase64FromFile());
request.enqueue(new Callback<Upload>() {
@Override
public void onResponse(Call<Upload> call, Response<Upload> response) {
}
@Override
public void onFailure(Call<Upload> call, Throwable t) {
}
});
}发布于 2018-09-21 17:40:41
尝试在上传之前压缩你的文件或图像,因为这将花费太多的时间
发布于 2018-09-21 17:58:54
首先,您正在使用改进的enqueue()方法,这是一种异步执行代码的方式,并且您已经注册了对这些方法的回调。如果成功执行,您将在onResponse()方法中收到调用,但在失败时,您将在onFailure()方法中获得控制权。
这将从守护程序线程中产生一个执行线程,它会创建另一个执行线程,您可能永远不会知道这个线程将在什么时候根据操作系统优先级执行。
使用execute()方法以同步的方式执行,然后检查响应时间,它会给出正确的结果。
https://stackoverflow.com/questions/52440805
复制相似问题