android应用

This commit is contained in:
rjb
2026-01-20 11:03:55 +08:00
parent f6568f252a
commit d59f015362
16 changed files with 1754 additions and 17 deletions

View 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")
}

View File

@@ -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>
)

View File

@@ -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
)

View File

@@ -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"
)

View File

@@ -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
}