Я пытаюсь загрузить файл .csv с помощью Retrofit 2.
Ниже команда curl работает хорошо:
curl -X POST "<Base_URL>/upload" -H "accept: application/json" -H "Device-Id: abcd"
-H "Content-Type: multipart/form-data" -F "file=@23May201811:16:17pm_data.csv;type=text/csv"
Моя реализация в Android ниже:
@Multipart
@Headers({"accept: application/json"})
@POST("rawdata/insecured/upload")
Call<ResponseBody> uploadRawFile(@Part MultipartBody.Part file, @Header("Device-Id") String header, @Part("description") RequestBody string);
RequestBody requestBody = RequestBody.create(MediaType.parse("text/csv"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("csv", file.getName(), requestBody);
RequestBody contentDescription = RequestBody.create(MultipartBody.FORM, "FileName");
Call<ResponseBody> apiCall = client.getApiService().uploadRawFile(body, device_Id, contentDescription);
И журнал Android:
07-03 23:30:27.240 14996-16885/bendmotionproj D/OkHttp: --> POST http://BaseUrl/upload http/1.1
07-03 23:30:27.241 14996-16885/bendmotionproj D/OkHttp: Content-Type: multipart/form-data; boundary=3380e202-b5da-4fef-b287-d2f75e645400
07-03 23:30:27.243 14996-16885/bendmotionproj D/OkHttp: Content-Length: 12699
accept: application/json
Device-Id: 9bea6afdd252682a
07-03 23:30:27.249 14996-16885/bendmotionproj D/OkHttp: --3380e202-b5da-4fef-b287-d2f75e645400
Content-Disposition: form-data; name="csvfile"; filename="Jul3,201811:30:25PM_data.csv"
Content-Type: text/csv
Content-Length: 12269
Sensor Address,Timestamp (S),Fused Angle,Flex Angle,Perp. Angle
C6:25:44:AF:CB:CD,0.0,-42.2,-36.4,7.1
C6:25:44:AF:CB:CD,0.05,-37.5,-33.2,3.6
C6:25:44:AF:CB:CD,0.1,-34.4,-30.9,0.6
C6:25:44:AF:CB:CD,0.15000000000000002,-32.5,-29.2,-2.7
C6:25:44:AF:CB:CD,0.2,-32.5,-29.2,-5.4
C6:25:44:AF:CB:CD,0.25,-29.4,-25.9,-7.0
C6:25:44:AF:CB:CD,0.30000000000000004,-26.5,-23.6,-7.0
C6:25:44:AF:CB:CD,0.35000000000000003,-22.8,-21.1,-4.3
C6:25:44:AF:CB:CD,0.4,-21.1,-21.1,-1.7
C6:25:44:AF:CB:CD,0.45,-21.1,-21.1,0.1
C6:25:44:AF:CB:CD,0.5,-21.1,-21.1,0.1
C6:25:44:AF:CB:CD,0.55,-23.9,-21.1,2.0
C6:25:44:AF:CB:CD,0.6000000000000001,-28.7,-26.1,2.0
C6:25:44:AF:CB:CD,0.65,-38.2,-33.8,2.0
C6:25:44:AF:CB:CD,0.7000000000000001,-53.2,-45.6,5.5
07-03 23:30:28.539 14996-16885/bendmotionproj D/OkHttp: <-- 400 http://BaseURL/upload (1280ms)
X-Application-Context: kinexcsapigateway:with-service-discovery:8080
Date: Tue, 03 Jul 2018 15:30:23 GMT
Content-Length: 0
Connection: close
<-- END HTTP (0-byte body)
07-03 23:30:28.555 14996-14996/bendmotionproj D/BaseResponse: Response{protocol=http/1.1, code=400, message=, url=http://BaseUrl/upload}
07-03 23:30:28.560 14996-14996/bendmotionproj D/TextView: setTypeface with style : 0
Сетевой ответ - 400. И даже вызов API не попадает на сервер. Недостающая часть - правильно указать данные формы в запросе на модификацию файла. Мне нужно знать, как использовать -F для загрузки файла. Любая помощь будет оценена.