gpt4 book ai didi

java - 尝试使用 Java 在 Wordpress 上发帖,但标题不显示

转载 作者:行者123 更新时间:2023-12-01 21:41:58 25 4
gpt4 key购买 nike

我正在尝试使用 API 在 Wordpress 上发帖。到目前为止,我已经有了这段代码,我可以发布帖子,但唯一发布的内容是帖子的内容,而不是标题。 KI 可以显示类别,但不能显示标题。我缺少什么?这是我到目前为止的代码:

public void connessione(){
try {

URL url = new URL(Costanti.URL_POST);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Authorization", "Bearer " + Costanti.BEARER_TOKEN);
urlConnection.setDoOutput(true);
PrintWriter wr = new PrintWriter(urlConnection.getOutputStream());

wr.append(this.creaPost());
wr.flush();
urlConnection.connect();
InputStream is = urlConnection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
StringBuilder response = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
System.out.println(response.toString());
} catch (Exception e) {
e.printStackTrace();
}


}

private String creaPost(){
StringBuilder s = new StringBuilder();
s.append("?");
s.append("title=").append("Title");
s.append("&content=").append("description");
s.append("&status=").append("publish");

return s.toString();
}

我想将数据作为请求正文而不是请求参数发送,但我不知道该怎么做。我还想尝试使用 JSON 库之类的东西将代码编写为 JSON 而不是 java 字符串,但 NetBeans 由于某种原因不会读取这些库,我也不知道如何解决这个问题。

这也是我从该方法得到的响应的 JSON:

{
"id": 73,
"date": "2019-11-10T10:09:55",
"date_gmt": "2019-11-10T10:09:55",
"guid": {
"rendered": "http://localhost/tesi/2019/11/10/73/",
"raw": "http://localhost/tesi/2019/11/10/73/"
},
"modified": "2019-11-10T10:09:55",
"modified_gmt": "2019-11-10T10:09:55",
"password": "",
"slug": "73",
"status": "publish",
"type": "post",
"link": "http://localhost/tesi/2019/11/10/73/",
"title": {
"raw": "",
"rendered": ""
},
"content": {
"raw": "description",
"rendered": "<p>description</p>\n",
"protected": false,
"block_version": 0
},
"excerpt": {
"raw": "",
"rendered": "<p>descrizione</p>\n",
"protected": false
},
"author": 1,
"featured_media": 0,
"comment_status": "open",
"ping_status": "open",
"sticky": false,
"template": "",
"format": "standard",
"meta": [],
"categories": [
1
],
"tags": [],
"permalink_template": "http://localhost/tesi/2019/11/10/%postname%/",
"generated_slug": "73",
"_links": {
"self": [
{
"href": "http://localhost/tesi/wp-json/wp/v2/posts/73"
}
],
"collection": [
{
"href": "http://localhost/tesi/wp-json/wp/v2/posts"
}
],
"about": [
{
"href": "http://localhost/tesi/wp-json/wp/v2/types/post"
}
],
"author": [
{
"embeddable": true,
"href": "http://localhost/tesi/wp-json/wp/v2/users/1"
}
],
"replies": [
{
"embeddable": true,
"href": "http://localhost/tesi/wp-json/wp/v2/comments?post=73"
}
],
"version-history": [
{
"count": 0,
"href": "http://localhost/tesi/wp-json/wp/v2/posts/73/revisions"
}
],
"wp:attachment": [
{
"href": "http://localhost/tesi/wp-json/wp/v2/media?parent=73"
}
],
"wp:term": [
{
"taxonomy": "category",
"embeddable": true,
"href": "http://localhost/tesi/wp-json/wp/v2/categories?post=73"
},
{
"taxonomy": "post_tag",
"embeddable": true,
"href": "http://localhost/tesi/wp-json/wp/v2/tags?post=73"
}
],
"wp:action-publish": [
{
"href": "http://localhost/tesi/wp-json/wp/v2/posts/73"
}
],
"wp:action-unfiltered-html": [
{
"href": "http://localhost/tesi/wp-json/wp/v2/posts/73"
}
],
"wp:action-sticky": [
{
"href": "http://localhost/tesi/wp-json/wp/v2/posts/73"
}
],
"wp:action-assign-author": [
{
"href": "http://localhost/tesi/wp-json/wp/v2/posts/73"
}
],
"wp:action-create-categories": [
{
"href": "http://localhost/tesi/wp-json/wp/v2/posts/73"
}
],
"wp:action-assign-categories": [
{
"href": "http://localhost/tesi/wp-json/wp/v2/posts/73"
}
],
"wp:action-create-tags": [
{
"href": "http://localhost/tesi/wp-json/wp/v2/posts/73"
}
],
"wp:action-assign-tags": [
{
"href": "http://localhost/tesi/wp-json/wp/v2/posts/73"
}
],
"curies": [
{
"name": "wp",
"href": "https://api.w.org/{rel}",
"templated": true
}
]
}
}

最佳答案

我解决了这个问题,但将 & 放在 'title=' 之前所以现在是这样的:

private String creaPost(){
StringBuilder s = new StringBuilder();
s.append("?");
s.append("&title=").append("Title");
s.append("&content=").append("description");
s.append("&status=").append("publish");

return s.toString();
}

而且效果很好

关于java - 尝试使用 Java 在 Wordpress 上发帖,但标题不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58787718/

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