我已經寫了一個Java程序(只使用主法)發送使用JSON HTML內容的表中插入數據。 我有我的對象在單獨的程序中的一些數據,我想以編程方式導入我的Atlassian Confluence頁面的。 我仍然只能在頁面內插入靜態HTML內容和數據。 我從未與json合作過。 如何在表格的行中插入數據。如何在Atlassian的合流頁面使用Java
public class Example {
\t private static final String BASE_URL = "www.example.com";
\t private static final String USERNAME = "xxxxx";
\t private static final String PASSWORD = "xxxxx";
\t private static final String ENCODING = "utf-8";
\t
\t static HttpResponse putPageResponse;
\t static HttpEntity putPageEntity = null;
\t private static String getContentRestUrl(final Long contentId,
\t \t \t final String[] expansions) throws UnsupportedEncodingException {
\t \t \t final String expand = URLEncoder.encode(StringUtils.join(expansions, ","), ENCODING);
\t \t return String
\t \t \t \t .format("%s/rest/api/content/%s?expand=%s&os_authType=basic&os_username=%s&os_password=%s",
\t \t \t \t \t \t BASE_URL, contentId, expand,
\t \t \t \t \t \t URLEncoder.encode(USERNAME, ENCODING),
\t \t \t \t \t \t URLEncoder.encode(PASSWORD, ENCODING));
\t }
\t public static void main(final String[] args) throws Exception {
\t \t final long pageId = 00000000;
\t \t HttpClient client = new DefaultHttpClient();
\t \t // Get current page version
\t \t String pageObj = null;
\t \t HttpEntity pageEntity = null;
\t \t try {
\t \t \t HttpGet getPageRequest = new HttpGet(getContentRestUrl(pageId,
\t \t \t \t \t new String[] { "body.storage", "version", "ancestors" }));
\t \t \t
\t \t \t HttpResponse getPageResponse = client.execute(getPageRequest);
\t \t \t
\t \t \t pageEntity = getPageResponse.getEntity();
\t \t \t pageObj = IOUtils.toString(pageEntity.getContent());
\t \t
\t \t } finally {
\t \t \t if (pageEntity != null) {
\t \t \t \t EntityUtils.consume(pageEntity);
\t \t \t }
\t \t }
\t \t // Parse response into JSON
\t \t JSONObject page = new JSONObject(pageObj);
\t \t try {
\t
\t \t **page.getJSONObject("body").getJSONObject("storage")
\t \t .put("value","<b>html content here<b>");**
\t \t <!--if i try to write any thing in second param of put() than it replace the all content of body with that.-->
\t \t
\t \t int currentVersion = page.getJSONObject("version").getInt("number");
\t \t page.getJSONObject("version").put("number", currentVersion + 1);
\t \t // Send update request
\t \t
\t \t \t HttpPut putPageRequest = new HttpPut(getContentRestUrl(pageId,
\t \t \t \t \t new String[] {}));
\t \t \t StringEntity entity = new StringEntity(page.toString(),
\t \t \t \t \t ContentType.APPLICATION_JSON);
\t \t \t putPageRequest.setEntity(entity);
\t \t \t putPageResponse = client.execute(putPageRequest);
\t \t \t System.out.println("");
\t \t \t EntityUtils.consume(putPageEntity);
\t \t
\t \t \t
\t \t } catch(Exception e) {
\t \t \t System.out.println("exception is: "+e);
\t \t }
\t }
}
** 如果任何一個份額,任何示例代碼,會在我非常有幫助。 我在我的頁面中有以下表格。
__________________________________________ |
| S.no |書名|書籍作者|發佈日期|價格
| _____ | ________ | ___________ | __________ | ______ |
在這個網站上還有很多其他的問題處理所有這些主題 - 例如,我想你可以在這裏快速搜索「Java REST」,並找到很多答案。 – mskfisher 2014-10-31 12:54:22
'REST - > JSON'聽起來很不錯。 – EpicPandaForce 2014-10-31 13:02:37