I’m relatively new to API integration and would appreciate some guidance on transitioning my code from using OkHttpClient and Gson to using Volley and JSONObject for API requests.
Initially, I developed a service request feature using OkHttpClient and Gson, and everything seemed to work fine. However, upon completing the implementation, I discovered that another part of the application relies on Volley as a library and JSONObject for making requests.
When I attempted to modify my code to use Volley instead of Gson and OkHttpClient, I encountered an error message: “E [93] NetworkUtility.shouldRetryException: Unexpected response code 307 for https://url/score”.
Could someone please help me understand what this error message means and how I can resolve it?
Thank you in advance for your assistance. If you need further clarification or code snippets, please let me know.
private suspend fun scoreAudioWithASR(audioUri: Uri, text: String): String {
val audioBuffer = uriToBase64(context, audioUri)
val requestDict = mapOf(
"lang_code" to "voice",
"gender" to "M",
"text" to text,
"audio" to audioBuffer,
"tag" to "test"
)
// inspect the data produced for debugging purposes
// saveRequestData(requestDict)
val gson = Gson()
val requestBody = gson.toJson(requestDict)
.toRequestBody("json; charset=utf-8".toMediaTypeOrNull())
val requestBodyJsonString = gson.toJson(requestDict)
Log.d("JSON Object", requestBodyJsonString)
val request = Request.Builder()
.url("https://url/score")
.post(requestBody)
.build()
val client = OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.build()
return withContext(Dispatchers.IO) {
try {
client.newCall(request).execute().use { response ->
if (!response.isSuccessful) throw IOException("Unexpected code $response")
val responseDict = gson.fromJson(response.body?.string(), Map::class.java)
val utteranceDp = responseDict["utterance_dp"] as Double
Log.d("utteranceDp",utteranceDp.toString() )
val utteranceThresholds = responseDict["utterance_thresholds"] as List<Double>
Log.d("utteranceThresholds",utteranceThresholds.toString() )
if (utteranceDp > utteranceThresholds[1]) "good" else "try again"
}
} catch (e: Exception) {
Log.e("ASRScore", "Error making ASR request", e)
"error message."
}
}
}```
the code I am trying to use with Volley is:
private suspend fun scoreAudioWithASR(
audioUri: Uri,
text: String,
): String {
return suspendCoroutine { continuation ->
val audioBuffer = uriToBase64(context, audioUri)
val url = “https://vosk.qfrency.com/score/”
val jsonObject = JSONObject().apply {
put("lang_code", "zul_kids")
put("gender", "M")
put("text", text)
put("audio", audioBuffer)
put("tag", "test")
}
// Convert JSON object to a formatted string
val jsonString = jsonObject.toString(4)
// Log the formatted JSON string
Log.d(“JSON Object”, jsonString)
val queue = Volley.newRequestQueue(context)
val jsonObjectRequest = JsonObjectRequest(
Request.Method.GET, url, jsonObject,
{ response ->
// Handle successful response
val responseDict = response as Map<*, *>
val utteranceDp = responseDict["utterance_dp"] as Double
val utteranceThresholds = responseDict["utterance_thresholds"] as List<Double>
val result = if (utteranceDp > utteranceThresholds[1]) {
"Wenze kahle!"
} else {
"Awenzanga kahle, sicela uzame futhi."
}
continuation.resume(result)
},
{ error ->
// Handle Volley error
Log.e("API Error", "Error fetching game configuration: ${error.message}")
continuation.resumeWithException(error)
}
)
queue.add(jsonObjectRequest)
}
}```
Then I get error : ” E [93] NetworkUtility.shouldRetryException: Unexpected response code 307 for https://url/score”
I tried to add the redirection got almost same error or the request run infinitely.
Sthesh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1