如何使用 Java 构建具有正确实体且不使用任何库的 http post 请求?

发布于 2024-10-19 00:18:17 字数 1620 浏览 2 评论 0原文

我应该如何构建实体来实现这个发布请求?

POST https://picasaweb.google.com/data/feed/api/user/userID/albumid/albumID/photoid/photoID

<entry xmlns='http://www.w3.org/2005/Atom'>
  <content>great photo!</content>
  <category scheme="http://schemas.google.com/g/2005#kind"
    term="http://schemas.google.com/photos/2007#comment"/>
</entry>

它来自: http://code.google.com /intl/zh-TW/apis/picasaweb/docs/2.0/developers_guide_protocol.html#AddComments

有人可以提供示例或任何提示吗? 非常感谢。

更新: 我在这里添加了我的代码:

        List<Header> headers = new ArrayList<Header>();
    headers.add(new BasicHeader("GData-Version", "2"));
    headers.add(new BasicHeader("Authorization", "GoogleLogin auth=" + mAuthToken));

    EntityTemplate entity = new EntityTemplate(new ContentProducer() {
        public void writeTo(OutputStream ostream) throws IOException {
            Writer writer = new OutputStreamWriter(ostream, "UTF-8");

            writer.write("\r\n");
            writer.write("<entry xmlns='http://www.w3.org/2005/Atom'>");
            writer.write("<content>" + comment + "</content>");
            writer.write("<category scheme=\"http://schemas.google.com/g/2005#kind\"\r\n");
            writer.write("term=\"http://schemas.google.com/photos/2007#comment\"/>");
            writer.write("</entry>\r\n");

            writer.flush();
        }
    });

仍然没有运气。有什么想法吗?

How should I build the entity to achieve this post request?

POST https://picasaweb.google.com/data/feed/api/user/userID/albumid/albumID/photoid/photoID

<entry xmlns='http://www.w3.org/2005/Atom'>
  <content>great photo!</content>
  <category scheme="http://schemas.google.com/g/2005#kind"
    term="http://schemas.google.com/photos/2007#comment"/>
</entry>

It's from:
http://code.google.com/intl/zh-TW/apis/picasaweb/docs/2.0/developers_guide_protocol.html#AddComments

Could someone provide an example or any tips?
Many thanks.

UPDATE:
I added my code here:

        List<Header> headers = new ArrayList<Header>();
    headers.add(new BasicHeader("GData-Version", "2"));
    headers.add(new BasicHeader("Authorization", "GoogleLogin auth=" + mAuthToken));

    EntityTemplate entity = new EntityTemplate(new ContentProducer() {
        public void writeTo(OutputStream ostream) throws IOException {
            Writer writer = new OutputStreamWriter(ostream, "UTF-8");

            writer.write("\r\n");
            writer.write("<entry xmlns='http://www.w3.org/2005/Atom'>");
            writer.write("<content>" + comment + "</content>");
            writer.write("<category scheme=\"http://schemas.google.com/g/2005#kind\"\r\n");
            writer.write("term=\"http://schemas.google.com/photos/2007#comment\"/>");
            writer.write("</entry>\r\n");

            writer.flush();
        }
    });

Still no luck. Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何以畏孤独 2024-10-26 00:18:17

这是使用 HttpClient 的示例代码。

我希望这条信息对您有所帮助。

// yourID
String userID = "";
String albumID = "";
String photoID = "";

HttpPost postRequest = new HttpPost(
    "https://picasaweb.google.com/data/feed/api/user/" + userID
    + "/albumid/" + albumID + "/photoid/" + photoID);

postRequest.addHeader(new BasicHeader("GData-Version", "2.0"));
postRequest.addHeader(new BasicHeader("Authorization",
    "GoogleLogin auth=" + mAuthToken));

String content = 
    "<entry xmlns='http://www.w3.org/2005/Atom'>"
    + "<content>" + comment + "</content>"
    + "<category scheme='http://schemas.google.com/g/2005#kind'"
    + " term='http://schemas.google.com/photos/2007#comment'/>"
    + "</entry>";

try {
    StringEntity entity = new StringEntity(content);
    entity.setContentType(new BasicHeader("Content-Type",
        "application/atom+xml"));
    postRequest.setEntity(entity);

    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response = httpclient.execute(postRequest);

} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

It is a sample code using HttpClient.

I hope this piece of information will be of help to you.

// yourID
String userID = "";
String albumID = "";
String photoID = "";

HttpPost postRequest = new HttpPost(
    "https://picasaweb.google.com/data/feed/api/user/" + userID
    + "/albumid/" + albumID + "/photoid/" + photoID);

postRequest.addHeader(new BasicHeader("GData-Version", "2.0"));
postRequest.addHeader(new BasicHeader("Authorization",
    "GoogleLogin auth=" + mAuthToken));

String content = 
    "<entry xmlns='http://www.w3.org/2005/Atom'>"
    + "<content>" + comment + "</content>"
    + "<category scheme='http://schemas.google.com/g/2005#kind'"
    + " term='http://schemas.google.com/photos/2007#comment'/>"
    + "</entry>";

try {
    StringEntity entity = new StringEntity(content);
    entity.setContentType(new BasicHeader("Content-Type",
        "application/atom+xml"));
    postRequest.setEntity(entity);

    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response = httpclient.execute(postRequest);

} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
痴情 2024-10-26 00:18:17

您可以使用“GDataAPI”和“Guava-libraries”。

PicasawebService myService
    = new PicasawebService("exampleCo-exampleApp-1"); // just id
myService.setUserCredentials(
    "[email protected]", "mypassword"); // your mailaddress, password

// change "username", "albumid" and "photoid"
URL feedUrl = new URL(
    "https://picasaweb.google.com/data/feed/api/"
    + "user/username/albumid/albumid/photoid/photoid"); 

CommentEntry myComment = new CommentEntry(); 
myComment.setContent(
    new PlainTextConstruct("great photo!")); // there is comment
myService.insert(feedUrl, myComment);

请参考以下网址。

  1. http://code.google.com/intl/ja /apis/picasaweb/docs/2.0/developers_guide_java.html
  2. http:// /code.google.com/p/gdata-java-client/downloads(GDataAPI 下载)
  3. http://code.google.com/p/guava-libraries/(番石榴库)

You can use "GDataAPI" and "Guava-libraries".

PicasawebService myService
    = new PicasawebService("exampleCo-exampleApp-1"); // just id
myService.setUserCredentials(
    "[email protected]", "mypassword"); // your mailaddress, password

// change "username", "albumid" and "photoid"
URL feedUrl = new URL(
    "https://picasaweb.google.com/data/feed/api/"
    + "user/username/albumid/albumid/photoid/photoid"); 

CommentEntry myComment = new CommentEntry(); 
myComment.setContent(
    new PlainTextConstruct("great photo!")); // there is comment
myService.insert(feedUrl, myComment);

Refere to following URL.

  1. http://code.google.com/intl/ja/apis/picasaweb/docs/2.0/developers_guide_java.html
  2. http://code.google.com/p/gdata-java-client/downloads (GDataAPI Download)
  3. http://code.google.com/p/guava-libraries/ (Guava-libraries)
够运 2024-10-26 00:18:17

您可以使用 apache httpcomponents 中的 HttpClient 来创建 http 请求。

此处查找教程。

You can use HttpClient from apache httpcomponents to create http requests.

Find the tutorials here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文