You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android(Java)调用AWS SageMaker端点发送JSON遇错求助

Android(Java)调用AWS SageMaker推理端点报错求助

我需要从Android(Java)应用向AWS SageMaker推理端点发送JSON并接收响应,但网上找到的方案均无效。AWS文档未明确所需导入类、依赖,以及Android Manifest和Gradle模块的修改方式,特此求助。

已创建AWS推理端点,需发送JSON触发图像推理,此前Python实现的参考代码如下:

body = json.dumps({"input_key": input_key, "webcam": webcam})
content_type = 'application/json'
ioc_response = client.invoke_endpoint(
        EndpointName=endpoint_name,
        Body=body,
        ContentType=content_type)

当前Java代码:

package Utils;

import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.services.sagemakerruntime.AmazonSageMakerRuntime;
import com.amazonaws.services.sagemakerruntime.AmazonSageMakerRuntimeClientBuilder;
import com.amazonaws.services.sagemakerruntime.model.InvokeEndpointRequest;
import com.amazonaws.services.sagemakerruntime.model.InvokeEndpointResult;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
public class SageMakerUtils {

    private final String json;

    public SageMakerUtils(String json) {
        this.json = json;
    }

    public String run(){
        AmazonSageMakerRuntime client = AmazonSageMakerRuntimeClientBuilder
                .standard()
                .withCredentials(new DefaultAWSCredentialsProviderChain())
                .build();

        InvokeEndpointRequest invokeEndpointRequest = new InvokeEndpointRequest();
        invokeEndpointRequest.setContentType("application/json");

        invokeEndpointRequest.setBody(ByteBuffer.wrap(this.json.getBytes(StandardCharsets.UTF_8)));
        invokeEndpointRequest.setEndpointName("my-endpoint-name");

        InvokeEndpointResult result = client.invokeEndpoint(invokeEndpointRequest);

        return StandardCharsets.UTF_8.decode(result.getBody()).toString();
    }
}

Gradle依赖配置:

implementation 'com.amazonaws:aws-java-sdk-sagemakerruntime:1.12.190'

构建时出现错误:

2 files found with path 'META-INF/DEPENDENCIES' from inputs:

  • C:\Users\MyPC.gradle\caches\modules-2\files-2.1\org.apache.httpcomponents\httpclient\4.5.13\e5f6cae5ca7ecaac1ec2827a9e2d65ae2869cada\httpclient-4.5.13.jar
  • C:\Users\MyPC.gradle\caches\modules-2\files-2.1\org.apache.httpcomponents\httpcore\4.4.13\853b96d3afbb7bf8cc303fe27ee96836a10c1834\httpcore-4.4.13.jar

添加以下packagingOptions配置后,出现新错误:

android {      
      packagingOptions {
        exclude("META-INF/DEPENDENCIES")
        exclude("META-INF/LICENSE")
        exclude("META-INF/LICENSE.txt")
        exclude("META-INF/license.txt")
        exclude("META-INF/NOTICE")
        exclude("META-INF/NOTICE.txt")
        exclude("META-INF/notice.txt")
        exclude("META-INF/ASL2.0")
        exclude("META-INF/*.kotlin_module") 
      }          
}

新错误信息:

No static field INSTANCE of type Lorg/apache/http/conn/ssl/AllowAllHostnameVerifier; in class Lorg/apache/http/conn/ssl/AllowAllHostnameVerifier; or its superclasses (declaration of 'org.apache.http.conn.ssl.AllowAllHostnameVerifier' appears in /system/framework/framework.jar!classes4.dex)

使用Android SDK 33,配置如下:

defaultConfig {
        applicationId "com.example.myapp"
        minSdk 33
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

因Android Studio下AWS SageMaker相关文档极少,恳请提供解决方案或指导。

内容的提问来源于stack exchange,提问作者cocojambo01

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 13:25:42