cuốn sách gpt4 ai đã làm

Android với Retrofit2 OkHttp3 - Lỗi POST nhiều phần

In lại Tác giả: Taklimakan Thời gian cập nhật: 2023-11-03 00:57:29 28 4
mua khóa gpt4 Nike

Tôi đang sử dụng Retrofit2 với OkHttp cho các yêu cầu HTTP trên Android. Ở đây tôi đang thực hiện yêu cầu POST với việc tải lên tài liệu. Tôi đang gặp lỗi sau:

D/OkHttp: <-- Lỗi máy chủ 500 http://api.drivewealth.io/v1/documents (4289ms)
D/OkHttp: Ngày: Thứ ba, ngày 11 tháng 4 năm 2017 03:29:48 GMT
D/OkHttp: Kiểm soát bộ đệm: phải xác nhận lại, không có bộ đệm, không lưu trữ
D/OkHttp: Loại nội dung: văn bản/html bộ ký tự=ISO-8859-1;
D/OkHttp: Máy chủ: Cầu cảng(9.2.17.v20160517)
D/OkHttp: Độ dài nội dung: 9323
D/OkHttp: Kết nối: duy trì
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp: Lỗi máy chủ 500
D/OkHttp:
D/OkHttp:

LỖI HTTP 500


D/OkHttp:

Sự cố khi truy cập /v1/documents.
D/OkHttp:

 Lỗi máy chủ

Gây ra bởi:

org.apache.cxf.interceptor.Lỗi: Không thể xác định ranh giới từ tin nhắn!
D/OkHttp: tại org.apache.cxf.interceptor.AttachmentInInterceptor.handleMessage(AttachmentInInterceptor.java:60)
D/OkHttp: tại org.apache.cxf.jaxrs.ext.MessageContextImpl.createAttachments(MessageContextImpl.java:279)
D/OkHttp: tại org.apache.cxf.jaxrs.ext.MessageContextImpl.get(MessageContextImpl.java:77)
D/OkHttp: tại org.apache.cxf.jaxrs.impl.tl.ThreadLocalMessageContext.get(ThreadLocalMessageContext.java:42)
D/OkHttp: tại org.apache.cxf.jaxrs.utils.multipart.AttachmentUtils.getMultipartBody(AttachmentUtils.java:114)
D/OkHttp: tại org.apache.cxf.jaxrs.utils.multipart.AttachmentUtils.getAttachments(AttachmentUtils.java:119)
Xem sitemap của VNExpress

Nhật ký gỡ lỗi hoàn chỉnh đã được tải lênđây

Máy chủ API yêu cầu các yêu cầu nhiều phần HTTP POST phải ở định dạng sau:

nhập mô tả hình ảnh ở đây

Đoạn mã của tôi như sau:

1) Tạo lớp Trình xử lý trang bị thêm:

Tiêu đề chặnInterceptor = new Interceptor() {
@Ghi đè
công khai chặn okhttp3.Response (chuỗi Interceptor.Chain) ném IOException {
Yêu cầu gốc = chain.request();

Chuỗi sessionKey = JStockApplication.instance().getTradingOptions().getSessionKey();

okhttp3.Request yêu cầu = original.newBuilder()
//.header("Chấp nhận", "application/json")
.header("Content-Type", "multipart/form-data")
.header("x-mysolomeo-session-key", sessionKey)
.method(origin.method(), original.body())
.xây dựng();

return chain.proceed(request);
}
};

OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

// thêm các tiêu đề chung tĩnh
httpClient.addInterceptor(headerInterceptor);

// thêm Ghi nhật ký để phát triển, Cấp độ nhật ký: NONE, BASIC, HEADERS, BODY
HttpLoggingInterceptor logInterceptor = HttpLoggingInterceptor mới()
.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClient.addInterceptor(logInterceptor);

Trình tạo Retrofit.Builder = new Retrofit.Builder()
.baseUrl("http://api.drivewealth.io/v1/")
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build());

Trang bị thêm trang bị thêm = builder.build();

DriveWealthApi api = retrofit.create(DriveWealthApi.class);

2) Lớp giao diện của phương thức Retrofit:

nhập okhttp3.MultipartBody;
nhập okhttp3.RequestBody;
nhập okhttp3.ResponseBody;
nhập khẩu trang bị thêm2.Call;
nhập retrofit2.http.Body;
nhập retrofit2.http.Multipart;
nhập retrofit2.http.POST;
nhập retrofit2.http.Part;

giao diện công cộng DriveWealthApi {
@Multipart
@POST("tài liệu")
Gọi addDocument(
@Part("mã thông báo") Mã thông báo requestBody,
@Part ("loại tài liệu") Loại tài liệu requestBody,
@Part Tệp MultipartBody.Part);
}

3) Trong lớp Fragment của tôi, hãy gọi yêu cầu POST trong onCreate():

lớp công khai AddDocumentTaskFragment mở rộng Fragment triển khai Callback {
@Ghi đè
khoảng trống công khai onCreate(Gói đã lưuInstanceState) {
........
........

Gói gói = this.getArguments();
Chuỗi userID = bó.getString(INTENT_EXTRA_USER_ID);
Chuỗi docType = bó.getString(INTENT_EXTRA_DOCUMENT_TYPE);
Chuỗi fileUri = bó.getString(INTENT_EXTRA_FILE_URI);
Uri uri = Uri.parse(fileUri);

Chuỗi filePath = MyUtils.getPath(this.getActivity(), uri);

if (filePath == null || filePath.isEmpty()) {
return;
}

Tệp cuối cùng myFile = Tệp mới (filePath);
MediaType mediaType = MediaType.parse(getActivity().getContentResolver().getType(uri));

nếu (myFile == null) {
return;
}

// tạo instance requestBody từ file
RequestBody requestFile =RequestBody.create(mediaType, myFile);

// MultipartBody.Part được sử dụng để gửi tên file thực tế
MultipartBody.Part fileBody = MultipartBody.Part.createFormData("documentImage", myFile.getName(), requestFile);

// thêm một phần khác trong yêu cầu nhiều phần
AskBody tokenBody =RequestBody.create(okhttp3.MultipartBody.FORM, userID);

RequestBody docTypeBody =RequestBody.create(okhttp3.MultipartBody.FORM, docType);

// thông số: token, documentType, file
this.call = driveWealthApi.addDocument(tokenBody, docTypeBody, fileBody);
this.call.enqueue(this);
}

@Ghi đè
public void onResponse(Gọi gọi, Phản hồi phản hồi) {
.....
}

@Ghi đè
public void onFailure(Call call, Throwable t) {
......
}

Có biết chuyện gì đang xảy ra ở đây không? Cảm ơn!

câu trả lời hay nhất

Trích dẫnRetrofit 2 không thể tải lên tệp có thêm hai tham số chuỗi riêng biệtCuối cùng, tôi đã triển khai nó theo đề xuất của @TommySM. Tôi đã giải quyết vấn đề của mình bằng cách này:

// tạo instance requestBody từ file
requestBody requestFile = requestBody.create(
loại phương tiện,
myFile);

// MultipartBody.Part được sử dụng để gửi tên file thực tế
Tệp MultipartBody.Part = MultipartBody.Part.createFormData(
"hình ảnh tài liệu",
myFile.getName(),
requestFile);

// thêm một phần khác trong yêu cầu nhiều phần
Mã thông báo requestBody = requestBody.create(
MediaType.parse("text/plain"), // Đã sửa ở đây
//okhttp3.MultipartBody.FORM, => CÓ LỖI
ID người dùng);

Loại tài liệu requestBody = requestBody.create(
MediaType.parse("text/plain"), // Đã sửa ở đây
//okhttp3.MultipartBody.FORM, => CÓ LỖI
docTypeStr);

// mã thông báo, loại tài liệu, tệp
this.call = driveWealthApi.addDocument(token, docType, file);

Các tham số chuỗi đó phải được chỉ định là Loại nội dung: văn bản/đồng bằng thay vì Loại nội dung: nhiều phần/biểu mẫu dữ liệu.

Xem ảnh chụp màn hình để biết chi tiết:

1) BÀI ĐĂNG có vấn đề

nhập mô tả hình ảnh ở đây

2) BÀI ĐĂNG Đúng

nhập mô tả hình ảnh ở đây

Về Android có Retrofit2 OkHttp3 - Lỗi POST nhiều phần, chúng tôi đã tìm thấy một câu hỏi tương tự trên Stack Overflow: https://stackoverflow.com/questions/43320205/

28 4 0
Xem sitemap của VNExpress