当以字符串形式发送文件时,如何显示上载进度?我了解了关于这个问题的所有可用帖子,但都使用了带有文件的多部分帖子。但我没有这个文件。我有一根非常非常大的绳子。请帮帮我,谢谢。
我添加了拦截器:
Interceptor uploadInterceptor = new Interceptor() {
@Override
public Response intercept (Chain chain) throws IOException {
Request originalRequest = chain.request();
if (originalRequest.body() == null) {
return chain.proceed(originalRequest);
} else {
Request progressRequest = originalRequest.newBuilder()
.method(originalRequest.method(), new ProgressRequestBody(originalRequest.body(), progressListener))
.build();
return chain.proceed(progressRequest);
}
}
此自定义请求主体:
public class ProgressRequestBody extends RequestBody {
private final String TAG = "ProgressRequestBody";
private RequestBody requestBody;
private final ProgressListener progressListener;
private CountingSink countingSink;
public ProgressRequestBody (RequestBody requestBody, ProgressListener progressListener) {
this.requestBody = requestBody;
this.progressListener = progressListener;
}
@Override
public MediaType contentType () {
Log.e(TAG, "requestBody.contentType = " + requestBody.contentType());
return requestBody.contentType();
}
@Override
public long contentLength () throws IOException {
try {
return requestBody.contentLength();
} catch (IOException e) {
Log.e(TAG, "contentLength error: " + e.getMessage());
}
return -1;
}
@Override
public void writeTo (BufferedSink sink) throws IOException {
countingSink = new CountingSink(sink);
BufferedSink bufferedSink = Okio.buffer(countingSink);
requestBody.writeTo(bufferedSink);
bufferedSink.flush();
}
protected final class CountingSink extends ForwardingSink {
private long bytesWritten = 0;
public CountingSink (Sink delegate) {
super(delegate);
}
@Override
public void write (Buffer source, long byteCount) throws IOException {
super.write(source, byteCount);
bytesWritten += byteCount;
progressListener.update(bytesWritten, contentLength());
}
}
}
改装服务:
@Multipart
@POST (FILE_ADD)
Call<JsonObject> add (@PartMap Map<String, RequestBody> params);
和上传方法:
public Observable<DownloadProgress> apiUploadFile (final String sessionKey, final String profileId, final String encodedData) {
return Observable.create(new Observable.OnSubscribe<DownloadProgress>() {
@Override
public void call (final Subscriber<? super DownloadProgress> subscriber) {
FileApi fileApi = ApiFactory.createRetrofitService(FileApi.class, new ProgressListener() {
@Override
public void update (long bytesRead, long contentLength) {
long percent = (100 * bytesRead) / contentLength;
subscriber.onNext(new DownloadProgress((int) percent));
}
}, false);
RequestBody sessionKeyBody = RequestBody.create(MediaType.parse("multipart/form-data"), sessionKey);
RequestBody profileIdBody = RequestBody.create(MediaType.parse("multipart/form-data"), profileId);
RequestBody encodedDataBody = RequestBody.create(MediaType.parse("multipart/form-data"), encodedData);
Map<String, RequestBody> map = new HashMap<>();
map.put(ApiConst.SESSION_KEY, sessionKeyBody);
map.put(ApiConst.PROFILE_ID, profileIdBody);
map.put(ApiConst.ENCODED_DATA, encodedDataBody);
fileApi.add(map).enqueue(new Callback<JsonObject>() {
@Override
public void onResponse (Call<JsonObject> call, Response<JsonObject> callResponse) {
if (callResponse.isSuccessful() == true) {
JsonObject jsonObject = callResponse.body();
boolean result = jsonObject.get(ApiConst.RESULT).getAsBoolean();
if (result == true) {
JsonObject response = jsonObject.getAsJsonObject(ApiConst.RESPONSE);
String url = response.get(ApiConst.URL).getAsString();
subscriber.onNext(new DownloadProgress(url));
subscriber.onCompleted();
} else {
subscriber.onError(new RuntimeException(jsonObject.get(ApiConst.RESPONSE).getAsString()));
}
} else {
subscriber.onError(new RuntimeException("server contact failed"));
}
}
@Override
public void onFailure (Call<JsonObject> call, Throwable throwable) {
subscriber.onError(throwable);
}
});
}
});
}
但进展只有一次100%发射
试试这个代码
From with encoded url you can use this way
@FormUrlEncoded
@POST("user/edit")
Call<User> createUser(@Field("first_name") String first, @Field("last_name") String last);
我确信,所有GET参数都正确写入。我想,问题在于我如何发送文件上传。 接口: 有人能帮我吗?
我有一个像下面的邮递员的形象。我如何在改型2中做同样的事情? 我这样声明接口:
这是我的密码。它正常工作。但我想上传其他图像类型,如png,jpeg等。因此,我想更改filename=\“file1.jpeg” 我也想同时发送不同数量的文件。 请帮我解决这个问题。谢谢你。
content_type:multipart/form-data;边界=50FbFeB3-3ABC-4F15-B130-CdCB7E3A0E4F 内容帖子:数组([文件]=>PNG IHDR L AlotofBinaryGibberish......snip[文件2]=>PNG IHDR L更多二进制Gibberish... 有人能给我指个正确的方向吗? 我尝试了这个改版(2.0beta2)多部分
我正在尝试使用改装上传视频,但无法生成正确的格式。这是我的邮递员请求: 邮递员请求:https://i.stack.imgur.com/rDC6s.png 这是APIendpoint调用: ,这也是“UploadVideoRequest”类: 最后这是我的电话: 但我仍然无法发送该请求
当我调用API时,我得到了一个AWS S3存储桶URL在s3 URL上,我试图通过改造上传文件,但我得到了以下错误 我正在通过PUT方法进行多部分上传。在postman上,它可以工作,但不能从Android 相关改造