android - 使用JSON从Android应用程序向Django服务器发送/接收.wav文件

标签 android python json django audio

我正在尝试将.wav文件从我的Android应用发送到Django服务器。主要问题是在服务器端不断出现此错误: wave.Error:文件未以RIFF ID开头

从客户端的 Angular 来看,这是将 test_audio.wav 文件转换为字节[] 的方式

HashMap<String, String> postParams = new HashMap<>();

InputStream inStream = testPronunciationView.getContext().getResources().openRawResource(R.raw.test_audio);
ByteArrayOutputStream out = new ByteArrayOutputStream();
BufferedInputStream in = new BufferedInputStream(inStream);

int read;
byte[] buff = new byte[1024];
while ((read = in.read(buff)) > 0) {
    out.write(buff, 0, read);
}
out.flush();
byte[] fileAudioByte = out.toByteArray();

// two options to transform in a string
// 1st option
String decoded = new String(fileAudioByte, "UTF-8");
// 2nd option
String decoded = toJSON(fileAudioByte);

// decoded = either one of above
postDataParams.put("Audio", decoded)

// ....
// prepare a POST request here to send to the server

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);

OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();

编辑:创建JSON字符串的方法:
public static String toJSON(Object object) throws JSONException, IllegalAccessException
{
    String str = "";
    Class c = object.getClass();
    JSONObject jsonObject = new JSONObject();
    for (Field field : c.getDeclaredFields()) {
        field.setAccessible(true);
        String name = field.getName();
        String value = String.valueOf(field.get(object));
        jsonObject.put(name, value);
    }
    System.out.println(jsonObject.toString());
    return jsonObject.toString();
}

在服务器端,我这样做:
audiofile_string = data['FileAudio']

audiofile_byte = list(bytearray(audiofile_string, 'utf8'))
temp_audiofile = tempfile.NamedTemporaryFile(suffix='.wav')
with open(temp_audiofile.name, 'wb') as output:
     output.write(''.join(str(v) for v in audiofile_byte))

# The following line throws the error
f = wave.open(temp_audiofile.name, 'r') # wave.py library

因此,我认为我在转换或电话联络中做错了什么。有什么建议吗?谢谢

最佳答案

您是否有尝试使用JSON进行操作的特定原因?您不能只将二进制数据填充到JSON字符串中。

如果可以避免使用JSON,则只需使用multipart / form-data请求通过HTTP POST二进制数据。

如果由于某种原因而无法使用JSON,则可以使用base64编码来实现。在您的Android应用中,您需要对二进制数据进行base64编码。这将导致一个字符串。然后,您可以将JSON中的此字符串发送到服务器。在服务器端,您将需要从JSON获取此base64编码的字符串,对base64进行解码,然后将其保存到文件(或您要对二进制数据进行的任何处理)。这是一些小例子。

客户端:

int read;
byte[] buff = new byte[1024];
while ((read = in.read(buff)) > 0) {
    out.write(buff, 0, read);
}
out.flush();
byte[] fileAudioByte = out.toByteArray();

String encodedString = Base64.encodeToString(fileAudioByte, Base64.DEFAULT);
encodedString是一个String,您将其添加到JSON以发送到服务器。

服务器端:
import base64
...
audiofile_string = data['FileAudio']
audiofile_byte= base64.b64decode(audiofile_string)
# audiofile_byte now contains the bytes of the audio file, proceed to write to disk

关于android - 使用JSON从Android应用程序向Django服务器发送/接收.wav文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33456804/

相关文章:

python - 在Google App Engine邮件API中设置背景图像

android - 使用 csv 文件填充 Room 数据库

java - 无法在新线程中从 FREContext 调用 getActivity()?

android - 将图像从一个 Activity 传递到另一个 Activity

python - Gevent 队列 CPU 使用率高

json - 将 Golang 的 JSON API 调用响应输出到 nextjs 前端

java - 将自定义设置添加到 PreferenceFragment

python - 请求模块的 PyInstaller SSL 错误 - 缺少模块 ssl (_ssl)

java - 如何使用 JSON 将此 PHP 解析为 Java?

android - 解析JSON数据并将其放入gridview中