簡體   English   中英

如何使用Java使用正確的實體構建http post請求而不使用任何庫?

[英]How to build a http post request with the correct entity with Java and not using any library?

我應該如何構建實體以實現此發布請求?

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

仍然沒有運氣。 任何想法?

這是使用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();
}

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

PicasawebService myService
    = new PicasawebService("exampleCo-exampleApp-1"); // just id
myService.setUserCredentials(
    "liz@gmail.com", "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/(Guava-libraries

您可以使用apache httpcomponents中的HttpClient來創建http請求。

這里找到教程

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM