android应用
This commit is contained in:
180
androidExample/README.md
Normal file
180
androidExample/README.md
Normal file
@@ -0,0 +1,180 @@
|
||||
# Android Agent调用示例
|
||||
|
||||
这是一个Android示例项目,演示如何调用情感分析Agent。
|
||||
|
||||
## 📋 项目结构
|
||||
|
||||
```
|
||||
androidExample/
|
||||
├── app/
|
||||
│ ├── src/
|
||||
│ │ └── main/
|
||||
│ │ ├── java/com/example/agentclient/
|
||||
│ │ │ ├── MainActivity.kt
|
||||
│ │ │ ├── AgentService.kt
|
||||
│ │ │ ├── models/
|
||||
│ │ │ │ ├── AgentRequest.kt
|
||||
│ │ │ │ ├── AgentResponse.kt
|
||||
│ │ │ │ └── ExecutionResponse.kt
|
||||
│ │ │ └── utils/
|
||||
│ │ │ └── ApiClient.kt
|
||||
│ │ └── res/
|
||||
│ │ ├── layout/
|
||||
│ │ │ └── activity_main.xml
|
||||
│ │ └── values/
|
||||
│ │ └── strings.xml
|
||||
│ └── build.gradle.kts
|
||||
├── build.gradle.kts
|
||||
├── settings.gradle.kts
|
||||
└── README.md
|
||||
```
|
||||
|
||||
## 🚀 快速开始
|
||||
|
||||
### 1. 配置API地址
|
||||
|
||||
在 `app/src/main/java/com/example/agentclient/utils/ApiClient.kt` 中修改:
|
||||
|
||||
```kotlin
|
||||
private const val BASE_URL = "http://your-server-ip:8037"
|
||||
```
|
||||
|
||||
### 2. 配置Agent ID
|
||||
|
||||
在 `MainActivity.kt` 中修改:
|
||||
|
||||
```kotlin
|
||||
private val AGENT_ID = "your-agent-id" // 情感分析Agent的ID
|
||||
```
|
||||
|
||||
### 3. 运行项目
|
||||
|
||||
1. 使用Android Studio打开项目
|
||||
2. 同步Gradle依赖
|
||||
3. 运行到Android设备或模拟器
|
||||
|
||||
## 📱 功能特性
|
||||
|
||||
- ✅ 用户登录
|
||||
- ✅ 调用Agent API
|
||||
- ✅ 实时显示执行状态
|
||||
- ✅ 显示Agent回复
|
||||
- ✅ 错误处理
|
||||
|
||||
## 🔧 依赖库
|
||||
|
||||
- Retrofit2:网络请求
|
||||
- OkHttp:HTTP客户端
|
||||
- Gson:JSON解析
|
||||
- Coroutines:异步处理
|
||||
|
||||
## 📝 使用说明
|
||||
|
||||
1. **登录**
|
||||
- 应用启动后会自动登录(使用配置的用户名和密码)
|
||||
- 登录成功后可以开始使用Agent
|
||||
|
||||
2. **发送消息**
|
||||
- 在输入框中输入文本
|
||||
- 点击"发送"按钮
|
||||
- 等待Agent处理并返回结果
|
||||
|
||||
3. **查看结果**
|
||||
- Agent的回复会显示在消息列表中
|
||||
- 执行状态会实时更新
|
||||
|
||||
## 🔑 API说明
|
||||
|
||||
### 登录API
|
||||
```
|
||||
POST /api/v1/auth/login
|
||||
Content-Type: application/x-www-form-urlencoded
|
||||
|
||||
username=admin&password=123456
|
||||
```
|
||||
|
||||
### 执行Agent API
|
||||
```
|
||||
POST /api/v1/executions
|
||||
Authorization: Bearer <token>
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"agent_id": "agent-id",
|
||||
"input_data": {
|
||||
"query": "用户输入",
|
||||
"USER_INPUT": "用户输入"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 获取执行状态API
|
||||
```
|
||||
GET /api/v1/executions/{execution_id}/status
|
||||
Authorization: Bearer <token>
|
||||
```
|
||||
|
||||
### 获取执行结果API
|
||||
```
|
||||
GET /api/v1/executions/{execution_id}
|
||||
Authorization: Bearer <token>
|
||||
```
|
||||
|
||||
## 🎯 示例:调用情感分析Agent
|
||||
|
||||
```kotlin
|
||||
// 1. 登录获取token
|
||||
val token = agentService.login("admin", "123456")
|
||||
|
||||
// 2. 执行Agent
|
||||
val execution = agentService.executeAgent(
|
||||
agentId = "sentiment-analysis-agent-id",
|
||||
userInput = "这个产品真的很棒!"
|
||||
)
|
||||
|
||||
// 3. 轮询获取结果
|
||||
while (true) {
|
||||
val status = agentService.getExecutionStatus(execution.id)
|
||||
if (status.status == "completed") {
|
||||
val result = agentService.getExecutionResult(execution.id)
|
||||
println("情感分析结果: ${result.output_data}")
|
||||
break
|
||||
}
|
||||
delay(1000) // 等待1秒
|
||||
}
|
||||
```
|
||||
|
||||
## 📦 构建要求
|
||||
|
||||
- Android Studio Hedgehog | 2023.1.1 或更高版本
|
||||
- JDK 17 或更高版本
|
||||
- Android SDK API 24 或更高版本
|
||||
- Gradle 8.0 或更高版本
|
||||
|
||||
## 🔒 安全注意事项
|
||||
|
||||
1. **不要硬编码密码**:生产环境应该使用安全的认证方式
|
||||
2. **使用HTTPS**:生产环境必须使用HTTPS
|
||||
3. **Token管理**:妥善保管和刷新Token
|
||||
4. **网络安全配置**:Android 9+需要配置网络安全策略
|
||||
|
||||
## 🐛 故障排除
|
||||
|
||||
### 问题1:网络连接失败
|
||||
- 检查API地址是否正确
|
||||
- 检查设备网络连接
|
||||
- 检查服务器是否运行
|
||||
|
||||
### 问题2:登录失败
|
||||
- 检查用户名和密码是否正确
|
||||
- 检查服务器认证服务是否正常
|
||||
|
||||
### 问题3:Agent执行失败
|
||||
- 检查Agent ID是否正确
|
||||
- 检查Agent是否已发布
|
||||
- 查看服务器日志获取详细错误信息
|
||||
|
||||
## 📚 相关文档
|
||||
|
||||
- [Agent使用说明](../Agent使用说明.md)
|
||||
- [API文档](../backend/API_DOCUMENTATION.md)
|
||||
70
androidExample/app/build.gradle.kts
Normal file
70
androidExample/app/build.gradle.kts
Normal file
@@ -0,0 +1,70 @@
|
||||
plugins {
|
||||
id("com.android.application")
|
||||
id("org.jetbrains.kotlin.android")
|
||||
}
|
||||
|
||||
android {
|
||||
namespace = "com.example.agentclient"
|
||||
compileSdk = 34
|
||||
|
||||
defaultConfig {
|
||||
applicationId = "com.example.agentclient"
|
||||
minSdk = 24
|
||||
targetSdk = 34
|
||||
versionCode = 1
|
||||
versionName = "1.0"
|
||||
|
||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
isMinifyEnabled = false
|
||||
proguardFiles(
|
||||
getDefaultProguardFile("proguard-android-optimize.txt"),
|
||||
"proguard-rules.pro"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
sourceCompatibility = JavaVersion.VERSION_17
|
||||
targetCompatibility = JavaVersion.VERSION_17
|
||||
}
|
||||
|
||||
kotlinOptions {
|
||||
jvmTarget = "17"
|
||||
}
|
||||
|
||||
buildFeatures {
|
||||
viewBinding = true
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
// Android Core
|
||||
implementation("androidx.core:core-ktx:1.12.0")
|
||||
implementation("androidx.appcompat:appcompat:1.6.1")
|
||||
implementation("com.google.android.material:material:1.10.0")
|
||||
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
|
||||
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.2")
|
||||
implementation("androidx.activity:activity-ktx:1.8.1")
|
||||
|
||||
// Coroutines
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3")
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
|
||||
|
||||
// Retrofit & OkHttp
|
||||
implementation("com.squareup.retrofit2:retrofit:2.9.0")
|
||||
implementation("com.squareup.retrofit2:converter-gson:2.9.0")
|
||||
implementation("com.squareup.okhttp3:okhttp:4.12.0")
|
||||
implementation("com.squareup.okhttp3:logging-interceptor:4.12.0")
|
||||
|
||||
// Gson
|
||||
implementation("com.google.code.gson:gson:2.10.1")
|
||||
|
||||
// Testing
|
||||
testImplementation("junit:junit:4.13.2")
|
||||
androidTestImplementation("androidx.test.ext:junit:1.1.5")
|
||||
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.example.agentclient.models
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
/**
|
||||
* Agent执行请求模型
|
||||
*/
|
||||
data class AgentExecutionRequest(
|
||||
@SerializedName("agent_id")
|
||||
val agentId: String,
|
||||
|
||||
@SerializedName("input_data")
|
||||
val inputData: Map<String, Any>
|
||||
)
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.example.agentclient.models
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
/**
|
||||
* 执行记录响应模型
|
||||
*/
|
||||
data class ExecutionResponse(
|
||||
val id: String,
|
||||
|
||||
@SerializedName("agent_id")
|
||||
val agentId: String?,
|
||||
|
||||
@SerializedName("workflow_id")
|
||||
val workflowId: String?,
|
||||
|
||||
val status: String,
|
||||
|
||||
@SerializedName("input_data")
|
||||
val inputData: Map<String, Any>?,
|
||||
|
||||
@SerializedName("output_data")
|
||||
val outputData: Any?,
|
||||
|
||||
@SerializedName("created_at")
|
||||
val createdAt: String
|
||||
)
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.example.agentclient.models
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
/**
|
||||
* Token响应模型
|
||||
*/
|
||||
data class TokenResponse(
|
||||
@SerializedName("access_token")
|
||||
val accessToken: String,
|
||||
|
||||
@SerializedName("token_type")
|
||||
val tokenType: String = "bearer"
|
||||
)
|
||||
@@ -0,0 +1,99 @@
|
||||
package com.example.agentclient.utils
|
||||
|
||||
import com.example.agentclient.models.*
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.logging.HttpLoggingInterceptor
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
import retrofit2.http.*
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
/**
|
||||
* API客户端配置
|
||||
*
|
||||
* 使用说明:
|
||||
* 1. 修改 BASE_URL 为你的服务器地址
|
||||
* 2. 确保服务器允许跨域请求(CORS)
|
||||
*/
|
||||
object ApiClient {
|
||||
// TODO: 修改为你的服务器地址
|
||||
private const val BASE_URL = "http://101.43.95.130:8037"
|
||||
|
||||
// 创建OkHttp客户端
|
||||
private val okHttpClient = OkHttpClient.Builder()
|
||||
.connectTimeout(30, TimeUnit.SECONDS)
|
||||
.readTimeout(60, TimeUnit.SECONDS)
|
||||
.writeTimeout(60, TimeUnit.SECONDS)
|
||||
.addInterceptor(HttpLoggingInterceptor().apply {
|
||||
level = HttpLoggingInterceptor.Level.BODY
|
||||
})
|
||||
.build()
|
||||
|
||||
// 创建Retrofit实例
|
||||
private val retrofit = Retrofit.Builder()
|
||||
.baseUrl(BASE_URL)
|
||||
.client(okHttpClient)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build()
|
||||
|
||||
// 创建API服务
|
||||
val agentService: AgentService = retrofit.create(AgentService::class.java)
|
||||
}
|
||||
|
||||
/**
|
||||
* Agent API接口
|
||||
*/
|
||||
interface AgentService {
|
||||
/**
|
||||
* 用户登录
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param password 密码
|
||||
* @return Token响应
|
||||
*/
|
||||
@FormUrlEncoded
|
||||
@POST("/api/v1/auth/login")
|
||||
suspend fun login(
|
||||
@Field("username") username: String,
|
||||
@Field("password") password: String
|
||||
): TokenResponse
|
||||
|
||||
/**
|
||||
* 执行Agent
|
||||
*
|
||||
* @param request Agent执行请求
|
||||
* @param token 认证Token
|
||||
* @return 执行记录
|
||||
*/
|
||||
@POST("/api/v1/executions")
|
||||
suspend fun executeAgent(
|
||||
@Body request: AgentExecutionRequest,
|
||||
@Header("Authorization") token: String
|
||||
): ExecutionResponse
|
||||
|
||||
/**
|
||||
* 获取执行状态
|
||||
*
|
||||
* @param executionId 执行ID
|
||||
* @param token 认证Token
|
||||
* @return 执行状态
|
||||
*/
|
||||
@GET("/api/v1/executions/{execution_id}/status")
|
||||
suspend fun getExecutionStatus(
|
||||
@Path("execution_id") executionId: String,
|
||||
@Header("Authorization") token: String
|
||||
): ExecutionStatusResponse
|
||||
|
||||
/**
|
||||
* 获取执行结果
|
||||
*
|
||||
* @param executionId 执行ID
|
||||
* @param token 认证Token
|
||||
* @return 执行详情
|
||||
*/
|
||||
@GET("/api/v1/executions/{execution_id}")
|
||||
suspend fun getExecutionResult(
|
||||
@Path("execution_id") executionId: String,
|
||||
@Header("Authorization") token: String
|
||||
): ExecutionDetailResponse
|
||||
}
|
||||
9
androidExample/build.gradle.kts
Normal file
9
androidExample/build.gradle.kts
Normal file
@@ -0,0 +1,9 @@
|
||||
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
||||
plugins {
|
||||
id("com.android.application") version "8.1.0" apply false
|
||||
id("org.jetbrains.kotlin.android") version "1.9.0" apply false
|
||||
}
|
||||
|
||||
tasks.register("clean", Delete::class) {
|
||||
delete(rootProject.buildDir)
|
||||
}
|
||||
18
androidExample/settings.gradle.kts
Normal file
18
androidExample/settings.gradle.kts
Normal file
@@ -0,0 +1,18 @@
|
||||
pluginManagement {
|
||||
repositories {
|
||||
google()
|
||||
mavenCentral()
|
||||
gradlePluginPortal()
|
||||
}
|
||||
}
|
||||
|
||||
dependencyResolutionManagement {
|
||||
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
|
||||
repositories {
|
||||
google()
|
||||
mavenCentral()
|
||||
}
|
||||
}
|
||||
|
||||
rootProject.name = "AgentClient"
|
||||
include(":app")
|
||||
Reference in New Issue
Block a user