opt ab json decode
Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
parent
fb02334b46
commit
d45def7cbe
@ -78,15 +78,8 @@ class AbModel {
|
||||
tags.clear();
|
||||
peers.clear();
|
||||
} else if (resp.body.isNotEmpty) {
|
||||
Map<String, dynamic> json;
|
||||
try {
|
||||
json = jsonDecode(utf8.decode(resp.bodyBytes));
|
||||
} catch (e) {
|
||||
if (resp.statusCode != 200) {
|
||||
throw 'HTTP ${resp.statusCode}, $e';
|
||||
}
|
||||
rethrow;
|
||||
}
|
||||
Map<String, dynamic> json =
|
||||
_jsonDecode(utf8.decode(resp.bodyBytes), resp.statusCode);
|
||||
if (json.containsKey('error')) {
|
||||
throw json['error'];
|
||||
} else if (json.containsKey('data')) {
|
||||
@ -221,7 +214,7 @@ class AbModel {
|
||||
});
|
||||
http.Response resp;
|
||||
// support compression
|
||||
if (licensedDevices > 0 && body.length > 1024) {
|
||||
if (licensedDevices > 0 && body.length > 102400) {
|
||||
authHeaders['Content-Encoding'] = "gzip";
|
||||
resp = await http.post(Uri.parse(api),
|
||||
headers: authHeaders, body: GZipCodec().encode(utf8.encode(body)));
|
||||
@ -229,12 +222,11 @@ class AbModel {
|
||||
resp =
|
||||
await http.post(Uri.parse(api), headers: authHeaders, body: body);
|
||||
}
|
||||
try {
|
||||
if (resp.statusCode == 200 &&
|
||||
(resp.body.isEmpty || resp.body.toLowerCase() == 'null')) {
|
||||
_saveCache();
|
||||
} else {
|
||||
final json = jsonDecode(resp.body);
|
||||
Map<String, dynamic> json = _jsonDecode(resp.body, resp.statusCode);
|
||||
if (json.containsKey('error')) {
|
||||
throw json['error'];
|
||||
} else if (resp.statusCode == 200) {
|
||||
@ -243,12 +235,6 @@ class AbModel {
|
||||
throw 'HTTP ${resp.statusCode}';
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
if (resp.statusCode != 200) {
|
||||
throw 'HTTP ${resp.statusCode}, $e';
|
||||
}
|
||||
rethrow;
|
||||
}
|
||||
} catch (e) {
|
||||
pushError.value =
|
||||
'${translate('push_ab_failed_tip')}: ${translate(e.toString())}';
|
||||
@ -469,4 +455,17 @@ class AbModel {
|
||||
debugPrint("load ab cache: $e");
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, dynamic> _jsonDecode(String body, int statusCode) {
|
||||
try {
|
||||
Map<String, dynamic> json = jsonDecode(body);
|
||||
return json;
|
||||
} catch (e) {
|
||||
final err = body.isNotEmpty && body.length < 128 ? body : e.toString();
|
||||
if (statusCode != 200) {
|
||||
throw 'HTTP $statusCode, $err';
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user