如果在 YARC REST 客戶端中使用顫振回應呼叫它,我會從 api 得到不同的回應:
Flutter 中的回應:
第一行是發送到 api 的 json。


api 日志中顯示的錯誤:
Failed to parse json data from JsonRpc@::ffff:192.168.0.243: Error: illegal value at Line: 0, Column: 0
主要飛鏢:
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_colorpicker/flutter_colorpicker.dart';
import 'package:smart_home/user_model.dart';
import 'package:http/http.dart' as http;
void main() {
runApp(const MyApp());
}
Future req(String col) async{
var data = json.encode({
"command":"color",
"color":[255,0,0],
"priority":50,
"origin":"My Fancy App"
});
print(data);
final response = await http.post(Uri.parse("http://192.168.0.151:8090/json-rpc"),
body: data,
headers: {"Authorization": "token 46a2032e-da1b-4c20-b690-27413aa43589"}
);
print(response.body);
if(response.statusCode==200){
final String responseString = response.body;
return userModelFromJson(responseString);
}
else
return null;
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
var selectedvalue = null;
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark(),
home: Scaffold(
appBar: AppBar(
title: Text("Led Controls",
style: TextStyle(fontSize: 25),),
),
body: Column(
children: [
Container(
padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
child: DropdownButton(
value: selectedvalue,
items: <String>['Static color', 'Effects'].map((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
onChanged: (value) {
setState(() {
selectedvalue=value.toString();
});
},
isExpanded: true,
hint: Text("Select Operating Mode"),
),
),
if(selectedvalue=="Static color") Container(
//padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
child: ColorPicker(
pickerColor: Colors.white,
paletteType: PaletteType.hueWheel,
onColorChanged: (x) async{
String col="[${x.red.toString()},${x.green.toString()},${x.blue.toString()}]";
final UserModel? resp = await req(col);
//print(resp!.success);
},
)
)
],
),
),
);
}
}
這是我的第一個顫振專案,所以我仍然不完全確定它是如何作業的。
更新:它在 YARC、郵遞員和 python 上運行良好。它不適用于高級休息客戶端(弧)和顫振。
uj5u.com熱心網友回復:
這是由 Hyperion 服務器中的錯誤引起的。它將 HTTP 標頭視為區分大小寫。(這違反了相關的 RFC。) Dart 總是強制標題為小寫。在這種情況下,服務器正在尋找Content-Length,但 Dart 發送content-length,服務器(錯誤地)沒有發現是同一件事。
幸運的是,Dart 中有一種方法可以強制使用標題的大小寫,但是您必須多寫幾行樣板代碼。這是一個作業示例:
import 'dart:convert';
import 'dart:io';
void main() async {
// encode the post body as JSON and then UTF8 bytes
final dataBytes = utf8.encode(json.encode({
'command': 'color',
'color': [255, 0, 0],
'priority': 50,
'origin': 'My Dart App'
}));
// use the low level HttpClient to get control of the header case
final client = HttpClient();
final request = await client.post('127.0.0.1', 8090, '/json-rpc');
// manually add the content length header to preserve its case
request.headers.add(
'Content-Length', // note the upper case string - try it with lower case (spoiler it fails)
dataBytes.length.toString(),
preserveHeaderCase: true,
);
// optional - add other headers (e.g. Auth) here
// request.headers.add(/*some other header*/);
// send the body bytes
request.add(dataBytes);
// 'close' the request to send it, and get a response
final response = await request.close();
print(response.statusCode);
// turn the streamed response back to a string so that it can be parsed as JSON
final responseBody = await response.transform(utf8.decoder).join();
print(responseBody);
// close the client (or re-use it if you prefer)
client.close();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/410321.html
標籤:
