我在Android上使用了一个Web服务“http://www.w3schools.com/webservices/tempconvert.asmx”并尝试访问其方法“CelsiusToFahrenheit”并在清单文件中添加了Internet权限但是当我运行时模拟器显示错误结果的项目,而不是将摄氏温度转换为华氏温度。任何帮助将不胜感激。 这是我的代码:
public class FinalKsoap extends Activity {
/** Called when the activity is first created. */
public static final String NAMESPACE = "http://tempuri.org/";
public static final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
public static final String URL =
"http://www.w3schools.com/webservices/tempconvert.asmx";
public static final String METHOD_NAME = "CelsiusToFahrenheit";
TextView tv,tv1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView)findViewById(R.id.text01);
tv1 = (TextView)findViewById(R.id.text02);
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
Request.addProperty("Celsius", "32");
SoapSerializationEnvelope soapEnvelope = new
SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(Request);
AndroidHttpTransport aht = new AndroidHttpTransport(URL);
try
{
aht.call(SOAP_ACTION, soapEnvelope);
SoapPrimitive result = (SoapPrimitive)soapEnvelope.getResponse();
tv.setText("Status : " + result);
}
catch(Exception e)
{
e.printStackTrace();
}
tv1.setText("Hellooooooo");
}
}
答案 0 :(得分:0)
这是一个常见的错误。据我所知,getResponse()方法总是只返回true(命令成功)或false(命令失败)。
如果您想要Web服务返回的数据,则必须使用以下代码:
public static void test() throws IOException, XmlPullParserException {
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
Request.addProperty("Celsius", "32");
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(Request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, soapEnvelope);
SoapPrimitive result = (SoapPrimitive) soapEnvelope.getResponse();
SoapObject resultsRequestSOAP = (SoapObject) soapEnvelope.bodyIn;
System.out.println(result); // prints "89.6"
if (resultsRequestSOAP != null) {
System.out.println(resultsRequestSOAP);
Object o = resultsRequestSOAP.getProperty("CelsiusToFahrenheitResult");
System.out.println(o); // prints "89.6"
}
}