fix: Android 版本发送按钮改为真实 API 调用,之前是写死的 mock 数据
This commit is contained in:
@@ -114,7 +114,6 @@ class MainActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
sendButton.setOnClickListener {
|
sendButton.setOnClickListener {
|
||||||
Log.d("MainActivity", "Send button clicked")
|
Log.d("MainActivity", "Send button clicked")
|
||||||
Log.e("MainActivity", "TEST ERROR LOG")
|
|
||||||
val inputText = inputEditText.text.toString()
|
val inputText = inputEditText.text.toString()
|
||||||
if (inputText.isNotEmpty()) {
|
if (inputText.isNotEmpty()) {
|
||||||
outputStatusLabel.text = "连接中…"
|
outputStatusLabel.text = "连接中…"
|
||||||
@@ -128,29 +127,73 @@ class MainActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
Log.d("MainActivity", "Full content to send: $fullContent")
|
Log.d("MainActivity", "Full content to send: $fullContent")
|
||||||
|
|
||||||
CoroutineScope(Dispatchers.Main).launch {
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
try {
|
try {
|
||||||
Log.d("MainActivity", "Starting text processing")
|
val sharedPrefs = getSharedPreferences("APIConfigs", Context.MODE_PRIVATE)
|
||||||
delay(1000)
|
val json = sharedPrefs.getString("configs", null)
|
||||||
|
|
||||||
val optimizedText = "今天阳光明媚,微风拂面,我漫步于公园之中,享受这难得的惬意时光。"
|
var baseUrl = "https://open.bigmodel.cn/api/paas/v4"
|
||||||
|
var apiKey = ""
|
||||||
|
var model = "glm-4.7-flash"
|
||||||
|
|
||||||
outputStatusLabel.text = "已完成"
|
if (json != null) {
|
||||||
outputTextView.text = optimizedText
|
val settings = Gson().fromJson(json, SettingsData::class.java)
|
||||||
|
settings.llmConfig?.let {
|
||||||
val selectedPromptId = when (promptSelector.selectedItemPosition) {
|
if (it.baseUrl.isNotBlank()) baseUrl = it.baseUrl
|
||||||
0 -> "none"
|
if (it.apiKey.isNotBlank()) apiKey = it.apiKey
|
||||||
1 -> "default-1"
|
if (it.model.isNotBlank()) model = it.model
|
||||||
2 -> "default-2"
|
}
|
||||||
else -> "none"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.d("MainActivity", "Selected prompt ID: $selectedPromptId")
|
val messagesJson = JSONArray().apply {
|
||||||
Log.d("MainActivity", "Text processing completed successfully")
|
put(JSONObject().apply {
|
||||||
|
put("role", "user")
|
||||||
|
put("content", fullContent)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
val requestBody = JSONObject().apply {
|
||||||
|
put("model", model)
|
||||||
|
put("messages", messagesJson)
|
||||||
|
put("max_tokens", 65536)
|
||||||
|
put("temperature", 1.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
val client = OkHttpClient()
|
||||||
|
val request = Request.Builder()
|
||||||
|
.url("$baseUrl/chat/completions")
|
||||||
|
.addHeader("Content-Type", "application/json")
|
||||||
|
.addHeader("Authorization", "Bearer $apiKey")
|
||||||
|
.post(requestBody.toString().toRequestBody("application/json".toMediaType()))
|
||||||
|
.build()
|
||||||
|
|
||||||
|
val response = client.newCall(request).execute()
|
||||||
|
|
||||||
|
withContext(Dispatchers.Main) {
|
||||||
|
if (response.isSuccessful) {
|
||||||
|
val responseBody = response.body?.string()
|
||||||
|
val responseJson = JSONObject(responseBody ?: "")
|
||||||
|
val choices = responseJson.getJSONArray("choices")
|
||||||
|
if (choices.length() > 0) {
|
||||||
|
val message = choices.getJSONObject(0).getJSONObject("message")
|
||||||
|
val content = message.getString("content")
|
||||||
|
outputStatusLabel.text = "已完成"
|
||||||
|
outputTextView.text = content
|
||||||
|
} else {
|
||||||
|
outputStatusLabel.text = "发生错误"
|
||||||
|
outputTextView.text = "API 返回空结果"
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
outputStatusLabel.text = "发生错误"
|
||||||
|
outputTextView.text = "API 错误: ${response.code} ${response.message}"
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
|
withContext(Dispatchers.Main) {
|
||||||
outputStatusLabel.text = "发生错误"
|
outputStatusLabel.text = "发生错误"
|
||||||
outputTextView.text = "错误: ${e.message}"
|
outputTextView.text = "错误: ${e.message}"
|
||||||
Log.e("MainActivity", "Error processing request", e)
|
}
|
||||||
|
Log.e("MainActivity", "Error calling API", e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user