Android套接字流媒体

Android套接字流媒体,android,websocket,httpurlconnection,ksoap2,Android,Websocket,Httpurlconnection,Ksoap2,已解决 我正在连接一个soap服务,该服务将向我的应用程序发布事件。我很难弄清楚如何收听更新的连接。下面是我的两个可能的连接函数,我将它们放在一起以连接到服务。我只是不知道接下来该怎么办 public HttpURLConnection Subscribe2() throws IOException { reset(); URL url = new URL("http://" + msHostAddx + "/services");

已解决 我正在连接一个soap服务,该服务将向我的应用程序发布事件。我很难弄清楚如何收听更新的连接。下面是我的两个可能的连接函数,我将它们放在一起以连接到服务。我只是不知道接下来该怎么办

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;
  }
public HttpURLConnection Subscribe2()引发IOException{
重置();
URL=新URL(“http://”+msHostAddx+“/services”);
conn=(HttpURLConnection)url
.openConnection();
conn.setRequestMethod(“POST”);
conn.setRequestProperty(“内容类型”,“text/xml;charset=utf-8”);
conn.setRequestProperty(“授权”、“基本”+
Base64.encodeToString((msUser+:“+msPassword.getBytes(),Base64.DEFAULT));
conn.setRequestProperty(“SOAPAction”,
SOAP(u行动),;
OutputStream reqStream=conn.getOutputStream();
字符串xmlRequest=“\n”+
“\n”+
“\n”+
“\n”+
“重用\u套接字\n”+
“无限\n”+
“\n”+
“\n”+
"";
write(xmlRequest.getBytes());
conn.setInstanceFollowRedirects(真);
返回连接;
}
public InputStream Subscribe()引发IOException{
重置();
试一试{
SoapObject请求=新的SoapObject(名称空间、方法名称);
addProperty(“reportURL”、“重用_套接字”);
请求。添加属性(“持续时间”、“无限期”);
SoapSerializationEnvelope=新的SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(请求);
envelope.env=”http://www.w3.org/2003/05/soap-envelope";
HttpTransportSE androidHttpTransport=新的HttpTransportSE(URL);
列表标题列表=新的ArrayList();
添加(新的HeaderProperty(“授权”、“基本”+org.kobjects.base64.base64.encode((msUser+“:“+msPassword.getBytes())));
androidHttpTransport.debug=true;
调用(SOAP_操作、信封、标题列表);
SoapObject结果=(SoapObject)envelope.getResponse();
//做些有反应的事情
}catch(XMLPullParseRexE){
e、 printStackTrace();
}
返回null;
}

我用下面的方法解决了这个问题

    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);
int content\u length=0;
StringBuffer headerBuffer=新StringBuffer();
int值;
做{
试一试{
而((charValue=reader.read())!=-1){//继续读取,直到输入结束
headerBuffer.append((char)charValue);
如果(charValue=='\n'){
int index=headerBuffer.length();
如果(索引>=4){//检查头数据的结尾
if(headerBuffer.charAt(索引-2)='\r'&&
headerBuffer.charAt(索引-3)='\n'&&headerBuffer.charAt(索引-4)='\r'){
content_length=getContentLength(headerBuffer.toString());
如果(内容长度<0)
打破
byte messageBuffer[]=新字节[内容长度];
int num_read=0;
做{
int r=reader.read(messageBuffer,num\u read,content\u length-num\u read);
如果(r==-1)
打破
num_read+=r;
}while(num_read