Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AndroidServiceConnection implements ServiceConnection {
- private static HttpConnectionManager connectionManager = new SimpleHttpConnectionManager();
- private HttpConnection connection;
- private PostMethod postMethod;
- private java.io.ByteArrayOutputStream bufferStream = null;
- /**
- * Constructor taking the url to the endpoint for this soap communication
- * @param url the url to open the connection to.
- */
- public AndroidServiceConnection(String url) throws IOException {
- HttpURL httpURL = new HttpURL(url);
- HostConfiguration host = new HostConfiguration();
- host.setHost(httpURL.getHost(), httpURL.getPort());
- connection = connectionManager.getConnection(host);
- postMethod = new PostMethod(url);
- }
- @Override
- public void connect() throws IOException {
- if (!connection.isOpen()) {
- connection.open();
- }
- }
- @Override
- public void disconnect() {
- connection.releaseConnection();
- }
- @Override
- public void setRequestProperty(String name, String value) {
- postMethod.setRequestHeader(name, value);
- }
- @Override
- public void setRequestMethod(String requestMethod) throws IOException {
- if (!requestMethod.toLowerCase().equals("post")) {
- throw(new IOException("Only POST method is supported"));
- }
- }
- @Override
- public OutputStream openOutputStream() throws IOException {
- bufferStream = new java.io.ByteArrayOutputStream();
- return bufferStream;
- }
- @Override
- public InputStream openInputStream() throws IOException {
- RequestEntity re = new ByteArrayRequestEntity(bufferStream.toByteArray());
- postMethod.setRequestEntity(re);
- postMethod.execute(new HttpState(), connection);
- return postMethod.getResponseBodyAsStream();
- }
- @Override
- public InputStream getErrorStream() {
- return null;
- }
- @Override
- public String getHost() {
- return null;
- }
- @Override
- public String getPath() {
- return null;
- }
- @Override
- public int getPort() {
- return 0;
- }
- @Override
- public List<?> getResponseProperties() throws IOException {
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement