已解决我正在连接到一个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;
}
我用下面的方法解决了这个问题。
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);