0

我正在使用 Google Blogger API 在我创建的博客中以编程方式创建帖子。我正在使用下面的代码尝试创建新帖子,但它没有添加新博客

编码:

private void addPost(){
new Thread (()->{
String mUserId = "MY_BLOGGER_POST_ID_HERE";
          String mYApiKey = "MY_API_KEY";
          String Authorization = "SOME_CHARACTERS_AND.apps.googleusercontent.com_HERE";

            

              final JSONObject obj = new JSONObject();
              obj.put("id", mUserId);

              final JSONObject requestBody = new JSONObject();
              requestBody.put("kind", "blogger#post");
              requestBody.put("blog", obj);
              requestBody.put("title", "TestingTheBlogOneTwo");
              requestBody.put("content", "ContentExample");

              DefaultHttpClient httpClient = new DefaultHttpClient();
              final HttpPost request = new HttpPost("https://www.googleapis.com/blogger/v3/blogs/" + mUserId + "/posts?key=" + mYApiKey + "");
              request.addHeader("Authorization", Authorization);
              request.addHeader("Content-Type", "application/json");
              request.setEntity(new StringEntity(requestBody.toString()));
    
              HttpResponse response = httpClient.execute(request);
              if (response.getStatusLine().getStatusCode() != 200){
                  Toast.makeText(getApplicationContext(), "status code " + response.getStatusLine().getStatusCode(), Toast.LENGTH_SHORT).show();
              }
              else {
                  Toast.makeText(getApplicationContext(), "done: ", Toast.LENGTH_SHORT).show();

              }
}).start();
}

但它没有添加任何帖子

•这是使用Blogger Api 添加帖子的正确方法吗?

•我遗漏了什么吗?

*请问我做错了什么。

任何帮助将不胜感激。提前致谢。

更新:应用程序没有崩溃,也没有添加新博客

4

1 回答 1

0

• 这种方法正确吗?

要知道这种方法是否“正确”,我们必须知道您对“正确”的定义是什么。由于您除了“这里有一个函数”之外没有添加任何信息,我们无法回答这个问题。

因为它返回null。

不,这不对。您已将其定义为:

Private void addPost(){

忽略Private(带有大写字母P)不是事物的事实,您的方法返回类型是void- 这意味着它什么都不返回。因此,根据定义,它不能是“返回 null”。


编辑:

•这是使用Blogger Api 添加帖子的正确方法吗?

您是否通读了 Blogger API 文档并按照说明完成了所有操作?

如果是这样,那么的。如果没有,那么没有

我遗漏了什么吗?

不知道如果遗漏了什么,任何人都应该知道。

伙计-您的实际问题是什么?“我做得对吗”或“我错过了什么”不是好问题。发生了什么让您带着这个问题来到 SO?有错误吗?有没有崩溃?你的笔记本电脑会爆炸吗?


请阅读:https ://stackoverflow.com/help/how-to-ask并尝试编辑您的问题,以便您真正提出问题并为试图帮助您给出答案的人提供足够的信息。

于 2021-09-20T04:32:19.960 回答