我是 Android Web 服务调用的新手。我正在尝试通过调用 SOAP Web 服务来开发一个显示当前温度的应用程序。当我尝试运行应用程序时,Logcat 中出现以下错误。
09-03 02:38:16.246: E/AndroidRuntime(1539): FATAL EXCEPTION: main
09-03 02:38:16.246: E/AndroidRuntime(1539): java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject
09-03 02:38:16.246: E/AndroidRuntime(1539): at com.example.updatesnew.MainActivity.onCreate(MainActivity.java:28)
MainActivity.java
public class MainActivity extends Activity
{
private static final String SOAP_ACTION = "http://ws.cdyne.com/WeatherWS/GetCityWeatherByZip";
private static final String METHOD_NAME = "GetCityWeatherByZip";
private static final String NAMESPACE ="http://ws.cdyne.com/WeatherWS/";
private static final String URL = "http://wsf.cdyne.com/WeatherWS/Weather.asmx?op=GetCityWeatherByZIP";
TextView tv;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView01);
SoapSerializationEnvelope soapEnvelope = null;
//Here I am getting error.
SoapObject Request = new SoapObject(NAMESPACE,METHOD_NAME);
Request.addProperty("Texas","75011");
soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(Request);
@SuppressWarnings("deprecation")
AndroidHttpTransport aht=new AndroidHttpTransport(URL);
try
{
aht.call(SOAP_ACTION,soapEnvelope);
SoapPrimitive resultString = (SoapPrimitive) soapEnvelope.getResponse();
tv.setText("Status: "+resultString);
}
catch (Exception e)
{
e.printStackTrace();
}
}
我也有一个疑问,无论Request.addProperty("Texas","75011");
是正确的方法与否,以获取邮政编码 75011 的德克萨斯城的当前温度。请指导我,如果可能的话,提供示例代码。
提前致谢。