From 90e1b2ce39268f53969f95c0f68324a63d19ae3d Mon Sep 17 00:00:00 2001 From: xiaji Date: Sun, 5 Apr 2026 20:53:45 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20Android=20=E7=89=88=E6=9C=AC=E5=8F=91?= =?UTF-8?q?=E9=80=81=E6=8C=89=E9=92=AE=E6=94=B9=E4=B8=BA=E7=9C=9F=E5=AE=9E?= =?UTF-8?q?=20API=20=E8=B0=83=E7=94=A8=EF=BC=8C=E4=B9=8B=E5=89=8D=E6=98=AF?= =?UTF-8?q?=E5=86=99=E6=AD=BB=E7=9A=84=20mock=20=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/example/flomo_ai/MainActivity.kt | 79 ++++++++++++++----- 1 file changed, 61 insertions(+), 18 deletions(-) diff --git a/flomo-ai/app/src/main/java/com/example/flomo_ai/MainActivity.kt b/flomo-ai/app/src/main/java/com/example/flomo_ai/MainActivity.kt index 44a1d6f..78893a5 100644 --- a/flomo-ai/app/src/main/java/com/example/flomo_ai/MainActivity.kt +++ b/flomo-ai/app/src/main/java/com/example/flomo_ai/MainActivity.kt @@ -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 {