Convert JsonObjectRequest to an Object

64 Views Asked by At

I'm trying to implement by own JSON search backend.

In ML Kit, the search engine uses Volley to process the request.

Once a barcode is detected in detectedBarcode this is ran:

LiveBarcodeScanningActivity.kt:

    workflowModel?.detectedBarcode?.observe(this, Observer { barcode ->
        if (barcode != null) {
            val barcodeFieldList = ArrayList<BarcodeField>()
            barcodeFieldList.add(BarcodeField("Raw Value", barcode.rawValue ?: ""))
            searchEngine!!.search(barcode) { productList, product ->
                workflowModel?.onSearchCompletedV2(product, productList)
            }
        }
    })

SearchEngine.kit

class SearchEngine(context: Context) {

private val searchRequestQueue: RequestQueue = Volley.newRequestQueue(context)
private val requestCreationExecutor: ExecutorService = Executors.newSingleThreadExecutor()

fun search(
        barcode: Barcode,
        listener: (productList: List<Product>, product: Product) -> Unit
) {
    Tasks.call<JsonObjectRequest>(requestCreationExecutor, Callable { createRequest(barcode) })
        .addOnSuccessListener { productRequest ->
             //Planning to map the JSON request data to the product object and add to the productList
            val product = Gson().fromJson(productRequest, Product::class.java) // None of the following functions can be called with the arguments supplied.
            listener.invoke(barcode, product)
        }
        .addOnFailureListener { e ->
            Log.e(TAG, "Failed to create product search request!", e)
        }
}

fun shutdown() {
    searchRequestQueue.cancelAll(TAG)
    requestCreationExecutor.shutdown()
}

companion object {
    private const val TAG = "SearchEngine"
    var url = "REQUEST URL"

    @Throws(Exception::class)
    private fun createRequest(barcode: Barcode): JsonObjectRequest {
        // Hooks up with your own product search backend here.
        return JsonObjectRequest(Request.Method.GET, url, null,
                { response ->
                    print("JSON request was a success$response")
                },
                { error ->
                    throw Exception("Failed to get product!$error")
                }
        )
    }
}

}

I'm getting "None of the following functions can be called with the arguments supplied." when I'm trying to convert from JsonObjectRequest to the Product object.

@Serializable
data class Product internal constructor(val imageUrl: String, val title: String, val subtitle: String)

Here's the Product object in which I'm trying to map the JSON data from my request to.

0

There are 0 best solutions below