Interaja com a REST API pública em um ServiceNow instância

  • Versão de lançamento: Zurich
  • Atualizado 31 de jul. de 2025
  • 1 min. de leitura
  • . Mobile SDK fornece funcionalidade que habilita Android Aplicações para chamar a REST API pública em um ServiceNow instância.

    Usando o. NowAPIService API Você pode interagir com qualquer uma das bases ServiceNow público REST APIs Ou crie REST APIs personalizadas na instância e chame-as do seu Android aplicação.

    Antes de fazer uma chamada para um ServiceNow REST API, você deve chamar MakeNowAPIService() método para criar uma instância do serviço. Se for bem-sucedido, a instância de serviço será retornada no retorno de chamada, caso contrário, um erro será gerado.

    A seguir mostra como inicializar um objeto NowAPIService:
    /**
     * Helper class used to handle different Now service instances. It has an application scope or is Singleton
     */
    @Singleton
    class SdkManager @Inject constructor() {
    
        private var nowApiService: NowAPIService? = null
    
        /**
         * Create the NowAPIService once in the lifetime of the application inside the Application class or another manager class
         * that will be injected into other classes via dagger/hilt.
         * NowAPIService should be created after initializing the NowSDK
         */
        suspend fun getNowApiService(): NowAPIService? {
            if (nowApiService != null) return nowApiService
    
            return NowDataSDK.makeAPIService(URL("https://instance-name.service-now.com")).getOrThrow()
                .also { this.nowApiService = it }
        }
    
    }
    Quando o objeto NowAPIService for inicializado, use o NowAPIService dados () Método para especificar o endpoint REST a ser chamado e todos os parâmetros associados:
     suspend fun makeNowApiCall() {
      val apiService = sdkManager.getNowApiService()
    
      val apiPath = "api/now/table/sn_customerservice_case"
      val endpoint = NowAPIService.Endpoint(HttpMethod.GET, apiPath, true)
      val fieldNames = "sys_id,number,short_description,number,priority,state," +
        "opened_at,account.name,account.number,contact.name,contact.email," +
        "contact_type,assignment_group.name,assigned_to.name"
    
      val queryParamsMap = mapOf("sysparm_fields" to fieldNames, "sysparm_limit" to "10")
    
      val queryParams = QueryParams.Builder().addAll(queryParamsMap).build()
    
      val response = runCatching {
        apiService?.data(endpoint = endpoint, queryParams = queryParams)?.execute()
      }
    
      if (response.isSuccess) {
        val resultString = response.getOrNull()?.body?.let { String(it) }
      } else {
        // Handle error
      }
    }