Android 发布带有改装的音频文件
问题是这个。我正在发送一个音乐文件。我不断收到错误Android 发布带有改装的音频文件,android,retrofit2,multipart,Android,Retrofit2,Multipart,问题是这个。我正在发送一个音乐文件。我不断收到错误422。由于这个原因,我似乎无法正确地定位我的身体 这是我从控制台的输出 Content-Disposition: form-data; name="Content-Disposition: form-data; name="audio[file]"; filename="tr.mp3"" Content-Transfer-Encoding: binary Content-Type:
422
。由于这个原因,我似乎无法正确地定位我的身体
这是我从控制台的输出
Content-Disposition: form-data; name="Content-Disposition: form-data;
name="audio[file]"; filename="tr.mp3""
Content-Transfer-Encoding: binary
Content-Type: audio/mpeg
Content-Length: 6028060
还有我的密码
@Multipart
@Headers({"Content-Type: multipart/form-data;", "Accept:application/json, text/plain, */*"})
@POST("audios")
Call<SoundResponse> saveSound(@Part ("Content-Disposition: form-data; name=\"audio[file]\"; filename=\"tr.mp3\"") RequestBody file,
@Query("auth_token") String authToken);
我还发现了这个东西在我看来,问题在于字段看起来像这样的“音频”[文件]”
感谢您的帮助我找到了问题的答案。解决方案是必须将文件转换为字节
private void sendFile(Uri data) {
mParent.showProgress();
MultipartBody.Part file = packFile(view.getContext(), "audio[file]", data);
GeoService.saveSound(file, SoundResponseCallback, getAuthToken());
}
@Nullable
public static MultipartBody.Part packFile(@NonNull Context context, @NonNull String partName, @Nullable Uri fileUri) {
if (fileUri == null) return null;
ContentResolver cr = context.getContentResolver();
String tp = cr.getType(fileUri);
if (tp == null) {
tp = "audio";
}
try {
InputStream iStream = context.getContentResolver().openInputStream(fileUri);
byte[] inputData = getBytes(iStream);
RequestBody requestFile = RequestBody.create(MediaType.parse(tp), inputData);
return MultipartBody.Part.createFormData(partName, fileUri.getLastPathSegment(), requestFile);
} catch (Exception e) {
return null;
}
}
@Nullable
private static byte[] getBytes(@Nullable InputStream inputStream) throws IOException {
if (inputStream == null) return null;
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}
return byteBuffer.toByteArray();
}
你是说你在发送一个音频文件,而不是从服务器请求它?所以我发送了这个文件
private void sendFile(Uri data) {
mParent.showProgress();
MultipartBody.Part file = packFile(view.getContext(), "audio[file]", data);
GeoService.saveSound(file, SoundResponseCallback, getAuthToken());
}
@Nullable
public static MultipartBody.Part packFile(@NonNull Context context, @NonNull String partName, @Nullable Uri fileUri) {
if (fileUri == null) return null;
ContentResolver cr = context.getContentResolver();
String tp = cr.getType(fileUri);
if (tp == null) {
tp = "audio";
}
try {
InputStream iStream = context.getContentResolver().openInputStream(fileUri);
byte[] inputData = getBytes(iStream);
RequestBody requestFile = RequestBody.create(MediaType.parse(tp), inputData);
return MultipartBody.Part.createFormData(partName, fileUri.getLastPathSegment(), requestFile);
} catch (Exception e) {
return null;
}
}
@Nullable
private static byte[] getBytes(@Nullable InputStream inputStream) throws IOException {
if (inputStream == null) return null;
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}
return byteBuffer.toByteArray();
}