1. Home
  2. Docs
  3. PixelSnapAI Documentation
  4. Example Code
  5. Kotlin

Kotlin

# PixelSnapAI API Kotlin Example

This documentation provides an example code snippet in Kotlin for using the PixelSnapAI API to upload and analyze an image. Follow the steps below to run the code successfully.

## Prerequisites

Before running the code, ensure that you have the following:

- Kotlin installed on your system.

## Code Example

```kotlin
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File

fun main() {
    val url = "URL_END_POINT"
    val token = "[SECRET_KEY]"
    val imageFile = File("./product_img.jpeg")
    
    val client = OkHttpClient()
    
    val requestBody = MultipartBody.Builder()
        .setType(MultipartBody.FORM)
        .addFormDataPart(
            "picture",
            imageFile.name,
            imageFile.asRequestBody("image/jpeg".toMediaType())
        )
        .build()
    
    val request = Request.Builder()
        .url(url)
        .addHeader("Authorization", "Token $token")
        .post(requestBody)
        .build()
    
    val response = client.newCall(request).execute()
    
    if (response.isSuccessful) {
        val responseBody = response.body?.string()
        // Parse the response JSON
        val jsonResponse = JSONObject(responseBody)
        // Extract the relevant information
        val message = jsonResponse.getString("message")
        val productNames = jsonResponse.getJSONArray("product_name")
        // Print the information
        println("Message: $message")
        println("Product Names: $productNames")
    } else {
        println("Error: ${response.code} ${response.message}")
    }
}

Running the Code

Follow these steps to run the provided code:

  1. Open a text editor or an integrated development environment (IDE) of your choice.
  2. Copy and paste the code into a new Kotlin file.
  3. Make sure you have the image file product_img.jpeg available in the same directory as your Kotlin file.
  4. Replace the placeholder value URL_END_POINT in the url variable with the actual PixelSnapAI API endpoint URL.
  5. Replace the placeholder value [SECRET_KEY] in the token variable with your actual PixelSnapAI API token. This token can be obtained from your PixelSnapAI account after subscribing to a plan.
  6. Save the file with a .kt extension, for example, PixelSnapAIExample.kt.
  7. Open a terminal or command prompt and navigate to the directory where the Kotlin file is saved.
  8. Compile and run the Kotlin file using the Kotlin compiler or an IDE with Kotlin support.
  9. The code will send a POST request to the PixelSnapAI API, uploading the image and returning the response.
  10. The response will be printed to the console, displaying the analysis results or any error messages received from the API.

Return Example

When the API request is successful, the code will receive a JSON response similar to the following:

jsonCopy code{
    "message": "Picture uploaded successfully.",
    "product_name": [
        "Dualshock 4",
        "PS4 DualShock 4",
        "Pro Wireless GamePad",
        "Nuc Controller Plus"
    ]
}

The returned JSON includes a "message" field indicating the success status and a "product_name" field containing a list of product names extracted from the uploaded picture.

Make sure to adjust the code as needed to handle the response according to your application’s requirements.

Conclusion

You have successfully run the Kotlin example code for utilizing the PixelSnapAI API to upload and analyze an image. This code demonstrates the process of making a POST request to the API endpoint and handling the response.

For further information on the available API endpoints, request parameters, and response formats, please refer to the official PixelSnapAI API documentation on the PixelSnapAI website.

If you encounter any issues or have additional questions, please contact our support team at info@pixelsnapai.com. We are here to assist you!

Was this article helpful to you? Yes No

How can we help?