fix: Android 版本发送按钮改为真实 API 调用,之前是写死的 mock 数据
This commit is contained in:
@@ -114,7 +114,6 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
sendButton.setOnClickListener {
|
||||
Log.d("MainActivity", "Send button clicked")
|
||||
Log.e("MainActivity", "TEST ERROR LOG")
|
||||
val inputText = inputEditText.text.toString()
|
||||
if (inputText.isNotEmpty()) {
|
||||
outputStatusLabel.text = "连接中…"
|
||||
@@ -128,29 +127,73 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
Log.d("MainActivity", "Full content to send: $fullContent")
|
||||
|
||||
CoroutineScope(Dispatchers.Main).launch {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
try {
|
||||
Log.d("MainActivity", "Starting text processing")
|
||||
delay(1000)
|
||||
val sharedPrefs = getSharedPreferences("APIConfigs", Context.MODE_PRIVATE)
|
||||
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 = "已完成"
|
||||
outputTextView.text = optimizedText
|
||||
|
||||
val selectedPromptId = when (promptSelector.selectedItemPosition) {
|
||||
0 -> "none"
|
||||
1 -> "default-1"
|
||||
2 -> "default-2"
|
||||
else -> "none"
|
||||
if (json != null) {
|
||||
val settings = Gson().fromJson(json, SettingsData::class.java)
|
||||
settings.llmConfig?.let {
|
||||
if (it.baseUrl.isNotBlank()) baseUrl = it.baseUrl
|
||||
if (it.apiKey.isNotBlank()) apiKey = it.apiKey
|
||||
if (it.model.isNotBlank()) model = it.model
|
||||
}
|
||||
}
|
||||
|
||||
Log.d("MainActivity", "Selected prompt ID: $selectedPromptId")
|
||||
Log.d("MainActivity", "Text processing completed successfully")
|
||||
val messagesJson = JSONArray().apply {
|
||||
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) {
|
||||
outputStatusLabel.text = "发生错误"
|
||||
outputTextView.text = "错误: ${e.message}"
|
||||
Log.e("MainActivity", "Error processing request", e)
|
||||
withContext(Dispatchers.Main) {
|
||||
outputStatusLabel.text = "发生错误"
|
||||
outputTextView.text = "错误: ${e.message}"
|
||||
}
|
||||
Log.e("MainActivity", "Error calling API", e)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user