gpt4 book ai didi

wordpress - 如何解决 flutter wordpress json api 错误?

转载 作者:IT王子 更新时间:2023-10-29 07:06:16 26 4
gpt4 key购买 nike

我正在尝试从我的 wordpress 网站解析 json http://crunchbox.epizy.com/wp-json/wp/v2/在一个从所有现有帖子中获取标题和特色图片的 flutter 应用程序上,代码是:

final String apiUrl = "http://crunchbox.epizy.com/wp-json/wp/v2/";
List posts;

Future<String> getPosts() async {
var res = await http.get(apiUrl,
headers: {"Accept": "application/json"});

setState(() {
var resBody = jsonDecode(res.body);
print(resBody);
posts = resBody;
});
return "Success!";

运行此代码后出现此错误:

E/flutter ( 2931): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter ( 2931): FormatException: Unexpected character (at character 1)
E/flutter ( 2931): <html><body><script type="text/javascript" src="/aes.js" ></script><script>...
E/flutter ( 2931): ^
E/flutter ( 2931):
E/flutter ( 2931): #0 _ChunkedJsonParser.fail (dart:convert/runtime/libconvert_patch.dart:1362:5)
E/flutter ( 2931): #1 _ChunkedJsonParser.parseNumber (dart:convert/runtime/libconvert_patch.dart:1258:9)
E/flutter ( 2931): #2 _ChunkedJsonParser.parse (dart:convert/runtime/libconvert_patch.dart:926:22)
E/flutter ( 2931): #3 _parseJson (dart:convert/runtime/libconvert_patch.dart:29:10)
E/flutter ( 2931): #4 JsonDecoder.convert (dart:convert/json.dart:542:36)
E/flutter ( 2931): #5 JsonCodec.decode (dart:convert/json.dart:169:41)
E/flutter ( 2931): #6 jsonDecode (dart:convert/json.dart:101:10)
E/flutter ( 2931): #7 _WordpressHomeState.getPosts.<anonymous closure> (file:///C:/Users/Arbaaz_AJ/IdeaProjects/wordpress_flutter/lib/main.dart:31:21)
E/flutter ( 2931): #8 State.setState (package:flutter/src/widgets/framework.dart:1125:30)
E/flutter ( 2931): #9 _WordpressHomeState.getPosts (file:///C:/Users/Arbaaz_AJ/IdeaProjects/wordpress_flutter/lib/main.dart:30:5)
E/flutter ( 2931): <asynchronous suspension>
E/flutter ( 2931): #10 _WordpressHomeState.initState (file:///C:/Users/Arbaaz_AJ/IdeaProjects/wordpress_flutter/lib/main.dart:41:10)
E/flutter ( 2931): #11 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3751:58)
E/flutter ( 2931): #12 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3617:5)
E/flutter ( 2931): #13 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2907:14)
E/flutter ( 2931): #14 Element.updateChild (package:flutter/src/widgets/framework.dart:2710:12)
E/flutter ( 2931): #15 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4654:14)
E/flutter ( 2931): #16 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2907:14)
E/flutter ( 2931): #17 Element.updateChild (package:flutter/src/widgets/framework.dart:2710:12)
E/flutter ( 2931): #18 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3653:16)
E/flutter ( 2931): #19 Element.rebuild (package:flutter/src/widgets/framework.dart:3495:5)
E/flutter ( 2931): #20 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3622:5)
E/flutter ( 2931): #21 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3617:5)
E/flutter ( 2931): #22 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2907:14)
E/flutter ( 2931): #23 Element.updateChild (package:flutter/src/widgets/framework.dart:2710:12)
E/flutter ( 2931): #24 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4654:14)
E/flutter ( 2931): #25 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2907:14)
E/flutter ( 2931): #26 Element.updateChild (package:flutter/src/widgets/framework.dart:2710:12)
E/flutter ( 2931): #27 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4654:14)
E/flutter ( 2931): #28 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2907:14)
E/flutter ( 2931): #29 Element.updateChild (package:flutter/src/widgets/framework.dart:2710:12)
E/flutter ( 2931): #30 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4654:14)
E/flutter ( 2931): #31 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2907:14)
E/flutter ( 2931): #32 Element.updateChild (package:flutter/src/widgets/framework.dart:2710:12)
E/flutter ( 2931): #33 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4654:14)
E/flutter ( 2931): #34 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2907:14)
E/flutter ( 2931): #35 Element.updateChild (package:flutter/src/widgets/framework.dart:2710:12)
E/flutter ( 2931): #36 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3653:16)
E/flutter ( 2931): #37 Element.rebuild (package:flutter/src/widgets/framework.dart:3495:5)
E/flutter ( 2931): #38 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3622:5)
E/flutter ( 2931): #39 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3769:11)
E/flutter ( 2931): #40 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3617:5)
E/flutter ( 2931): #41 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2907:14)
E/flutter ( 2931): #42 Element.updateChild (package:flutter/src/widgets/framework.dart:2710:12)
E/flutter ( 2931): #43 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3653:16)
E/flutter ( 2931): #44 Element.rebuild (package:flutter/src/widg

我想要实现的是在 ListView 中获取标题和摘录这里是代码:

FadeInImage.memoryNetwork(
placeholder: kTransparentImage,
image: posts[index]["featured_media"] == 0
? 'images/placeholder.png'
: posts[index]["_embedded"]["wp:featuredmedia"][0]
["source_url"],
),
Padding(
padding: EdgeInsets.all(10.0),
child: ListTile(
title: Padding(
padding: EdgeInsets.symmetric(vertical: 10.0),
child: Text(posts[index]["title"]["rendered"]),
),
subtitle: Text(posts[index]["excerpt"]["rendered"]
.replaceAll(new RegExp(r'<[^>]*>'), '')),
),

请帮助我,谢谢。

最佳答案

您的 API 正在返回一个 html 响应作为其机器人保护的一部分。响应实际上是一些代码,需要在服务器允许它访问 API 之前在您的客户端上运行。

这是它返回的 html(不是故意格式化的):

<html><body><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("..................."),b=toNumbers(".................."),c=toNumbers("...........");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; location.href="http://crunchbox.epizy.com/wp-json/wp/v2/posts?i=1";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html>

重要的是中间的javascript:

function toNumbers(d) {
var e = [];
d.replace(/(..)/g, function(d) {
e.push(parseInt(d, 16))
});
return e
}

function toHex() {
for (var d = [], d = 1 == arguments.length && arguments[0].constructor == Array ? arguments[0] : arguments, e = "", f = 0; f < d.length; f++) e += (16 > d[f] ? "0" : "") + d[f].toString(16);
return e.toLowerCase()
}
var a = toNumbers("..............."),
b = toNumbers("..............."),
c = toNumbers("...............");
document.cookie = "__test=" + toHex(slowAES.decrypt(c, 2, a, b)) + "; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/";
location.href = "http://crunchbox.epizy.com/wp-json/wp/v2/posts?i=1"; < /script>

请注意,我已将 toNumber("abcd1234") 中的三个二进制序列替换为....`。

如果您有权访问为 wordpress 网站提供服务的 NGINX 服务器(我假设),您可以禁用它。或者您可以使用 wordpress 的身份验证并使用插件添加额外的身份验证方法 - 请参阅 wordpress documentation for api authentication 的底部。

第三种选择是您可以“运行”js 代码并使用 header 将 cookie 设置到您的请求中。显然 flutter 无法原生运行 js。但理论上你可以在 android 和 ios 上使用 webviews 来完成它,或者你可以解析代码以找到 'a'、'b' 和 'c' 并使用某种库执行 AES-128 cbc 解密( this < em>可能有效,但没有任何 promise )。

关于wordpress - 如何解决 flutter wordpress json api 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52176399/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com