Android:以编程方式创建和发送 XML SOAP 请求
提前致谢.我找到了这个教程,我正在尝试模仿它:http://lalit3686.blogspot.com/2012/06/calling-soap-webservice-using-httppost.html.在我的 iOS 应用程序中,我能够创建一个具有 xml 属性的字符串,将其作为 SOAP 请求发送,并以字符串形式获取 xml 响应.我正在尝试用我的 android 应用做同样的事情.
Hi and thanks in advance. I found this tutorial and I'm trying to emulate it: http://lalit3686.blogspot.com/2012/06/calling-soap-webservice-using-httppost.html. In my iOS app I'm able to create a string with xml properties, send it as a SOAP request, and get an xml response in string form. I'm trying to do the same thing with my android app.
不幸的是,有些东西不起作用.我附上了相关代码.
Unfortunately something isn't working. I've attached relevant code.
StringBuilder stringBuilder = new StringBuilder ();
stringBuilder.append(String.format("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" +
"xmlns:csr=\"http://www.mywebsite.net/\">\n" +
"<soapenv:Header/>\n" +
"<soapenv:Body>\n" +
"<csr:www.mywebsite.net><![CDATA[\n" +
"<www.mywebsite.net>\n" +
"<Request>\n" +
"<user>%s</user>\n" +
"<password>%s</password>\n" +
"<Function>requestdata</Function>\n" +
"<FromDate>2013-01-01</FromDate>\n" +
"<ToDate>2014-01-01</ToDate>\n" +
"</Request>\n" +
"</www.mywebsite.net>\n" +
"]]></csr:www.mywebsite.net>\n" +
"</soapenv:Body>\n" +
"</soapenv:Envelope>\n", username, password));
String xml = stringBuilder.toString();
HttpPost httpPost = new HttpPost("http://www.mywebsite.net/");
StringEntity entity;
String response_string = null;
try {
entity = new StringEntity(xml, HTTP.UTF_8);
httpPost.setHeader("Content-Type","text/xml;charset=UTF-8");
httpPost.setEntity(entity);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(httpPost);
response_string = EntityUtils.toString(response.getEntity());
Log.d("request", response_string);
prefs.edit().putString("response", response_string).commit();
}
catch (Exception e) {
e.printStackTrace();
}
问题已修复.我收到错误:
problem fixed. I was getting the error:
android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099).
上面的代码有效 - 只需确保将此代码包含在您的 onCreate() 方法中.
The code above works - just make sure to include this code in your onCreate() method.
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
这允许您将主线程用于 SOAP 请求/响应.
This allows you to use the main thread for SOAP requests/responses.
我收到错误:
android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099).
android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099).
我在谷歌上搜索了这个错误并找到了这个链接:DefaultHttpClient to AndroidHttpClient - 这建议我包括在我的 onCreate 方法中使用以下代码.
I googled this error and came upon this link: DefaultHttpClient to AndroidHttpClient - which suggested I include the following code in my onCreate method.
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
现在可以工作了.事实上,Android 不再让网络操作在主线程上进行.
Now its working. In fact, Android does not let network operations be performed on the main thread any longer.