मैं रेट्रोफिट 2 के माध्यम से एक मल्टीपार्ट पोस्ट अनुरोध करने की कोशिश कर रहा हूं, जहां मैं एपीआई को एक कस्टम फ़ाइल अपलोड करता हूं।रेट्रोफिट। java.net.ProtocolException: अपेक्षित * बाइट्स लेकिन प्राप्त *
यह बेतरतीब ढंग से इस अपवाद के साथ विफल:
W/System.err: java.net.ProtocolException: expected 154 bytes but received 634
किसी को भी इस पर कुछ प्रकाश डाल सकता है करता है?
@Multipart
@POST("recordings/{id}/{rec_id}/")
Call<ResponseBody> uploadRecording(@Path("id") String id, @Path("rec_id") String rec_id, @Part MultipartBody.Part bleFile);
निर्माता में:
इस इंटरफ़ेस में मेरी कोड है
public ApiConnectionManager(Context con){
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.create();
OkHttpClient.Builder client = new OkHttpClient.Builder();
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
client.addInterceptor(loggingInterceptor);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(con.getResources().getString(R.string.api_url)) // API url is hidden
.addConverterFactory(GsonConverterFactory.create(gson))
.client(client.build())
.build();
this.companyAPI = retrofit.create(CompanyAPI.class);
}
और अपलोड विधि में:
private void uploadFile(String id, final File bleFile) {
MediaType MEDIA_TYPE = MediaType.parse("multipart/mixed");
RequestBody requestBody = RequestBody.create(MEDIA_TYPE,bleFile);
MultipartBody.Part partFile = MultipartBody.Part.createFormData("file", bleFile.getName(), requestBody);
String recordingId = bleFile.getName().replace(".BLE","");
Call<ResponseBody> call = companyAPI.uploadRecording(id, recordingId, partFile);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Log.d(TAG+"-Upload "+bleFile.getName(),response.message());
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.d(TAG,"FAILED");
t.printStackTrace();
}
});
}
"@Part" के बजाय एनोटेशन "@PartMap" का उपयोग करके समस्या हल हो गई। – kike
क्या आप कृपया इस उत्तर में call.enqueue() जोड़ सकते हैं ताकि एक ही समस्या वाले लोगों को कोड का एक कामकाजी टुकड़ा हो सके? – kike
@kike मैंने अपना जवाब बदल दिया है और यह भी जोड़ा है कि फाइलों के अलावा स्ट्रिंग कैसे भेजें। –