解决 我正在连接一个肥皂服务,将事件发布到我的应用程序。我很难搞清楚如何收听更新的连接。以下是我连接到服务的两个可能的连接函数。我不知道从哪里开始。
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;
}
答案 0 :(得分:0)
我能用以下解决这个问题。
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);