Android 用の SOAP クエリ リクエストを w3schools サーバーに送信しようとしました。そこで温度が摂氏から華氏に変換されますが、サーバーから次の応答が返されます。
Server was unable to process request.
---> Data at the root level is invalid.
Line 1, position 1.
サーバーは http://w3schools.com/webservices/tempconvert.asmxにあります。また、SOAP 要求メッセージの構造はhttp://w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheitにあります。これが私のコードです。
public class MainActivity extends Activity {
private static final String URL =
"http://www.w3schools.com/webservices/tempconvert.asmx";
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
StringBuffer sb = new StringBuffer("" +
"POST /webservices/tempconvert.asmx HTTP/1.1" +
"Host: w3schools.com" +
//"Content-Type: text/xml; charset=utf-8" +
//"Content-Length: length" +
"SOAPAction: \"http://tempuri.org/CelsiusToFahrenheit\"" +
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " +
"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
"<soap:Body>" +
"<CelsiusToFahrenheit xmlns=\"http://tempuri.org/\">" +
"<Celsius>24</Celsius>" +
"</CelsiusToFahrenheit>" +
"</soap:Body>" +
"</soap:Envelope>");
HttpPost request = new HttpPost(URL);
request.setHeader("Content-Type", "application/soap+xml; charset=utf-8");
tv = (TextView) findViewById(R.id.textView1);
try {
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("myxml", sb.toString()));
request.setEntity(new UrlEncodedFormEntity(pairs));
response = httpclient.execute(request);
String response_string = EntityUtils.toString(response.getEntity());
//Here we should parse the response xml
//But for quicker way, we will display the whole response.
tv.setText("Temperature in Fahrenheit is: " + response_string);
Log.v("responseString", response_string)
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
皆さんが私を助けていただければ幸いです。ありがとうございました!