【发布时间】:2022-11-26 18:22:21
【问题描述】:
我正在尝试通过 REST API 从我的 android 应用程序发布到我的自定义 Wordpress 字段。也就是说,当我查看任何 ACF 字段的 JSON 结构时,它们嵌套在“acf”中,如下所示:
{
"acf": {
"phone_number": "000-0000"
}
}
我正在尝试使用以下代码/结构将电话号码发布到端点的 phone_number 字段,但它似乎无法保存?
OkHttpClient client = new OkHttpClient();
String url = "http://myurl.com/wp-json/wp/v2/users/loggedinuser/36";
String bearer = "Bearer ";
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("phone_number", "777-348-4349");
} catch (JSONException e) {
e.printStackTrace();
}
RequestBody body = RequestBody.create(JSON, jsonObject.toString());
Request request = new Request.Builder()
.url(mergeUrl)
.method("POST", body)
.addHeader("Accept-Charset", "application/json")
.addHeader("Authorization", bearer + userToken)
.addHeader("Content-Type", "application/json")
.build();
Response response = null;
try {
response = client.newCall(request).execute();
String resStr = response.body().string();
int responseCode = response.code();
if (responseCode == 200) {
} else {
}
} catch (IOException | JSONException e) {
e.printStackTrace();
}
return null;
}}
我想这是因为 phone_number 嵌套在“acf”中。我该如何写这一行:
jsonObject.put("phone_number", "777-348-4349");
以便它匹配上面的结构?我似乎无法弄清楚如何将 phone_number 嵌套在 acf 值中。
【问题讨论】:
-
你能试试我的解决方案吗?
标签: java wordpress advanced-custom-fields wordpress-rest-api