0

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.

Toby
  • 63
  • 1
  • 8
  • Do you need help in creating data class? – idevesh Mar 13 '21 at 17:38
  • @idevesh Yes, I'm trying to convert my JSON request into the Product class – Toby Mar 13 '21 at 17:42
  • Does your problem solved with the below answer? – idevesh Mar 14 '21 at 07:34
  • @idevesh May do, I'll currently working on how to implement it. Do you think that's all that needs to be done? – Toby Mar 14 '21 at 12:24
  • Sorry, I thought you need to generate the class from the JSON Request. Because your question tells like that. You may try to generate a class using this website and check that it doesn't generate errors. https://www.json2kotlin.com – idevesh Mar 14 '21 at 13:36

0 Answers0