3

I need to send a file type "audio" (mp3) via POST request (I'm using Volley) in Kotlin language for Android. Basically I save a file, and everything is ok (I save it, I can also listen it and it works very well), now I need to send it to my server (e.g. "serverX.com") as ByteArray but I get always "error 502".

I've read also other posts, but I haven't solved anything. I use (and want to use) Volley for this request. I've tried in this way, so using "Files.readAllBytes..." but it doesn't work.

var encoded: ByteArray = Files.readAllBytes(Paths.get(this.output!!)) //file path is in "output" variable
var url_temp = "https://server-example.com/"

try {
  val path = "sends"
  val params = JSONArray()
  params.put(encoded)
  val que = Volley.newRequestQueue(this)
  val req = object : JsonArrayRequest(Request.Method.POST, temp_url + path, params,
    Response.Listener {
      val json_result = it.toString()
      println("Successful: " + it.toString())
    }, Response.ErrorListener {
      error2()
      println("Error: " + it.toString())
    }
  ) {
    @Throws(AuthFailureError::class)
    override fun getHeaders(): Map<String, String> {
      val headers = HashMap<String, String>()
      //something here
      return headers
    }
  }
  que.add(req)
} catch (e: Exception) {
  error2()
  println("Exception: " + e.toString())
}
MrMe
  • 59
  • 7

1 Answers1

-1

I've fixed this: the issue wasn't the request, but it was the encoding

val encoded = File(externalCacheDir, "$idSentence.aac").readBytes()
MrMe
  • 59
  • 7