Advertisement
Guest User

Untitled

a guest
May 23rd, 2012
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. public class AndroidServiceConnection implements ServiceConnection {
  2. private static HttpConnectionManager connectionManager = new SimpleHttpConnectionManager();
  3. private HttpConnection connection;
  4. private PostMethod postMethod;
  5. private java.io.ByteArrayOutputStream bufferStream = null;
  6.  
  7. /**
  8. * Constructor taking the url to the endpoint for this soap communication
  9. * @param url the url to open the connection to.
  10. */
  11. public AndroidServiceConnection(String url) throws IOException {
  12. HttpURL httpURL = new HttpURL(url);
  13. HostConfiguration host = new HostConfiguration();
  14. host.setHost(httpURL.getHost(), httpURL.getPort());
  15. connection = connectionManager.getConnection(host);
  16. postMethod = new PostMethod(url);
  17. }
  18.  
  19. @Override
  20. public void connect() throws IOException {
  21. if (!connection.isOpen()) {
  22. connection.open();
  23. }
  24. }
  25.  
  26. @Override
  27. public void disconnect() {
  28. connection.releaseConnection();
  29. }
  30.  
  31. @Override
  32. public void setRequestProperty(String name, String value) {
  33. postMethod.setRequestHeader(name, value);
  34. }
  35.  
  36. @Override
  37. public void setRequestMethod(String requestMethod) throws IOException {
  38. if (!requestMethod.toLowerCase().equals("post")) {
  39. throw(new IOException("Only POST method is supported"));
  40. }
  41. }
  42.  
  43. @Override
  44. public OutputStream openOutputStream() throws IOException {
  45. bufferStream = new java.io.ByteArrayOutputStream();
  46. return bufferStream;
  47. }
  48.  
  49. @Override
  50. public InputStream openInputStream() throws IOException {
  51. RequestEntity re = new ByteArrayRequestEntity(bufferStream.toByteArray());
  52. postMethod.setRequestEntity(re);
  53. postMethod.execute(new HttpState(), connection);
  54. return postMethod.getResponseBodyAsStream();
  55. }
  56.  
  57. @Override
  58. public InputStream getErrorStream() {
  59. return null;
  60. }
  61.  
  62. @Override
  63. public String getHost() {
  64. return null;
  65. }
  66.  
  67. @Override
  68. public String getPath() {
  69. return null;
  70. }
  71.  
  72. @Override
  73. public int getPort() {
  74. return 0;
  75. }
  76.  
  77. @Override
  78. public List<?> getResponseProperties() throws IOException {
  79. return null;
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement