title: Sending/receiving .wav file from Android app to Django server using JSON
body: <p>I am trying to send a .wav file from my Android app to a Django server. The main problem is that on the server side constantly get this error: <strong>wave.Error: file does not start with RIFF id</strong> </p>
  
  <p>From the Client point of view, this is the way I convert the <strong>test_audio.wav</strong> file to <strong>byte[]</strong></p>
  
  <pre><code>HashMap&lt;String, String&gt; postParams = new HashMap&lt;&gt;();
  
  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)) &gt; 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();
  </code></pre>
  
  <p><strong>EDIT:</strong> Method to create the JSON string:</p>
  
  <pre><code>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();
  }
  </code></pre>
  
  <p>On the server side I do:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>So I think that I am doing something wrong in the conversion or in the post call. Any suggestion ? Thanks</p>