Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. private static final String REST_SERVICE_URL = "http://rnguy.no-ip.biz:8080/com.KNASK.TodayInTheCity.11.21.00.36/rest/show";
  2. private static final XmlFactory f = new XmlFactory(new InputFactoryImpl(), new OutputFactoryImpl());
  3. private static final JacksonXmlModule module = new JacksonXmlModule();
  4. private static final XmlMapper xmlMapper = new XmlMapper(f, module);
  5.  
  6. @Override
  7. public int create(Show show) {
  8. try {
  9.  
  10. URL currentURL = null;
  11.  
  12. try {
  13. currentURL = new URL(REST_SERVICE_URL);
  14. } catch (MalformedURLException e) {
  15.  
  16. }
  17.  
  18. HttpURLConnection conn = (HttpURLConnection) currentURL.openConnection();
  19. conn.setDoOutput(true);
  20. conn.setRequestMethod("POST");
  21. conn.setRequestProperty("Content-Type", "application/xml");
  22.  
  23. OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream());
  24.  
  25. xmlMapper.writeValue(osw, show);
  26.  
  27. if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
  28. throw new RuntimeException("Failed : HTTP error code : "
  29. + conn.getResponseCode());
  30. }
  31.  
  32. BufferedReader br = new BufferedReader(new InputStreamReader(
  33. (conn.getInputStream())));
  34.  
  35. String output;
  36. System.out.println("Output from Server .... \n");
  37. while ((output = br.readLine()) != null) {
  38. System.out.println(output);
  39. }
  40.  
  41. conn.disconnect();
  42.  
  43. } catch (IOException e) {
  44. e.printStackTrace();
  45. }
  46.  
  47. return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement