MobileiOSAndroidFlutterReact Native
移动端 AI 集成:在 iOS/Android App 中调用中国大模型
如何在 iOS 和 Android 原生应用或跨平台框架(Flutter/React Native)中集成 ChinaWHAPI 的 AI 能力。
移动端安全注意
不要将 API Key 硬编码在 App 中,会被反编译获取。正确做法:通过后端代理或使用服务端签名的临时 Token。
Flutter 集成
import 'package:http/http.dart' as http;
import 'dart:convert';
Future<String> chat(String prompt) async {
final res = await http.post(
Uri.parse('https://chinawhapi.com/v1/chat/completions'),
headers: {
'Authorization': 'Bearer {server_token}', // 服务端 Token
'Content-Type': 'application/json',
},
body: jsonEncode({
'model': 'qwen3.5-flash',
'messages': [{'role': 'user', 'content': prompt}],
}),
);
return jsonDecode(res.body)['choices'][0]['message']['content'];
}React Native 集成
使用 openai SDK(React Native 支持)或直接用 fetch 调用 API。建议通过后端代理增加安全性。
网络优化
移动端网络不稳定,建议实现:请求超时(建议 30-60 秒)、自动重试(最多 2 次)、离线队列(无网络时暂存请求,恢复后重发)。