Search code examples
androidwebsockethttpurlconnectionksoap2

Android socket streaming


SOLVED I am connecting to a soap service that will post events to my application. I am having difficulty figuring out how to listen to the connection for the updates. Here are my 2 possible connection functions that I have put together to connect to the service. I am just not sure where to go from here.

public HttpURLConnection Subscribe2() throws IOException {
            reset();
            URL url = new URL("http://" + msHostAddx + "/services");
            conn = (HttpURLConnection) url
                .openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-type", "text/xml; charset=utf-8");
            conn.setRequestProperty("Authorization", "basic " +
                Base64.encodeToString((msUser + ":" + msPassword).getBytes(), Base64.DEFAULT));
            conn.setRequestProperty("SOAPAction",
                SOAP_ACTION);
            OutputStream reqStream = conn.getOutputStream();
            String xmlRequest = "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:ns=\"http://www.universal-devices.com/wsdk/isy/3.0\">\n" +
                "<soap:Header/>\n" +
                "<soap:Body>\n" +
                "<ns:Subscribe>\n" +
                "<reportURL>REUSE_SOCKET</reportURL>\n" +
                "<duration>infinite</duration>\n" +
                "</ns:Subscribe>\n" +
                "</soap:Body>\n" +
                "</soap:Envelope>";
            reqStream.write(xmlRequest.getBytes());
            conn.setInstanceFollowRedirects(true);
            return conn;
    }

public InputStream Subscribe() throws IOException {
    reset();
    try {

      SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
      request.addProperty("reportURL","REUSE_SOCKET");
      request.addProperty("duration","infinite");

      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
      envelope.dotNet = true;
      envelope.setOutputSoapObject(request);
      envelope.env = "http://www.w3.org/2003/05/soap-envelope";

      HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
      List<HeaderProperty> headerList = new ArrayList<>();

      headerList.add(new HeaderProperty("Authorization", "Basic " + org.kobjects.base64.Base64.encode((msUser+":"+msPassword).getBytes())));
      androidHttpTransport.debug = true;

      androidHttpTransport.call(SOAP_ACTION, envelope, headerList);


      SoapObject result = (SoapObject)envelope.getResponse();
      //Do something with response

    } catch (XmlPullParserException e) {
      e.printStackTrace();
    }
    return null;
  }

Solution

  • I was able to solve this with the below.

        int content_length = 0;
        StringBuffer headerBuffer = new StringBuffer();
        int charValue;
        do {
          try {
            while ((charValue = reader.read()) != -1) { //CONTINUE READING TILL END OF INPUT
              headerBuffer.append((char) charValue);
              if (charValue == '\n') {
                int index = headerBuffer.length();
                if (index >= 4) { //Check for end of header data
                  if (headerBuffer.charAt(index - 2) == '\r' &&
                      headerBuffer.charAt(index - 3) == '\n' && headerBuffer.charAt(index - 4) == '\r') {
                    content_length = getContentLength(headerBuffer.toString());
    
                    if (content_length < 0)
                      break;
                    byte messageBuffer[] = new byte[content_length];
                    int num_read = 0;
                    do {
                      int r = reader.read(messageBuffer, num_read, content_length - num_read);
                      if (r == -1)
                        break;
                      num_read += r;
                    } while (num_read < content_length);
                    //DO STUFF with messageBuffer
                    break;
                  }
                }
              }
            }
          } catch (Exception e) {
            e.printstacktrace();
          }
          headerBuffer.setLength(0);
        } while (true);