如何使用Java使用正确的实体构建http post请求而不使用任何库? [英] How to build a http post request with the correct entity with Java and not using any library?

查看:156
本文介绍了如何使用Java使用正确的实体构建http post请求而不使用任何库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何构建实体以实现此帖子请求?

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>

来自:
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?

推荐答案

这是一个使用HttpClient的示例代码。

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();
}

这篇关于如何使用Java使用正确的实体构建http post请求而不使用任何库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆