こんにちは、よろしくお願いします。このチュートリアルを見つけて、エミュレートしようとしています: http://lalit3686.blogspot.com/2012/06/calling-soap-webservice-using-httppost.html。私の iOS アプリでは、xml プロパティを含む文字列を作成し、それを SOAP リクエストとして送信し、xml レスポンスを文字列形式で取得できます。Androidアプリで同じことをしようとしています。
残念ながら、何かが機能していません。関連するコードを添付しました。
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();
}
編集:問題が修正されました。エラーが発生しました:
android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099).
上記のコードは機能します - このコードを onCreate() メソッドに必ず含めてください。
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
これにより、SOAP 要求/応答にメイン スレッドを使用できます。