可以和知你客服通信

This commit is contained in:
rjb
2026-03-07 13:59:49 +08:00
parent a789321005
commit 599b8f2851
6 changed files with 133 additions and 7 deletions

View File

@@ -4,6 +4,8 @@ import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import org.json.JSONObject;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
@@ -97,18 +99,41 @@ public class AgentChatActivity extends AppCompatActivity {
appendBotMessage(reply);
scrollToBottom();
} else {
Toast.makeText(AgentChatActivity.this, "请求失败: " + response.message(), Toast.LENGTH_SHORT).show();
String errMsg = getErrorMessage(response);
Toast.makeText(AgentChatActivity.this, errMsg, Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<ApiService.AgentChatResponse> call, Throwable t) {
binding.btnSend.setEnabled(true);
Toast.makeText(AgentChatActivity.this, "请求失败: " + t.getMessage(), Toast.LENGTH_SHORT).show();
String msg = t.getMessage();
if (msg == null || msg.isEmpty()) msg = "网络异常";
Toast.makeText(AgentChatActivity.this, "知你客服暂不可用: " + msg, Toast.LENGTH_LONG).show();
}
});
}
/** 从 502/503 等错误响应中解析后端返回的 error 文案 */
private String getErrorMessage(Response<ApiService.AgentChatResponse> response) {
try {
if (response.errorBody() != null) {
String body = response.errorBody().string();
if (!body.isEmpty()) {
JSONObject jo = new JSONObject(body);
if (jo.has("error")) {
return jo.optString("error", "知你客服暂不可用,请稍后重试");
}
}
}
} catch (Exception ignored) { }
int code = response.code();
if (code == 502 || code == 503) {
return "知你客服暂不可用,请稍后重试";
}
return "请求失败: " + response.message();
}
private void appendUserMessage(String content) {
ApiService.Message m = new ApiService.Message();
m.id = "local_u_" + System.currentTimeMillis();