Using Volley instead of Gson and OkHttpClient to request service:

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.

New contributor

Sthesh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật