今天在网上看到一个提供免费的天气预报服务,用JAVA调用这个服务的时候总是抛异常
望那位能够帮我解决一下  万分感谢
 
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;public class SelfInvoke 
{
public static void main(String [] args)
{
String theCityName="南京"; 
try {
String endpoint="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";
Service service = new Service();
Call call = (Call)service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.addParameter( new javax.xml.namespace.QName("http://WebXml.com.cn/", "theCityName"), org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
//call.addParameter(new javax.xml.namespace.QName("http://WebXml.com.cn/", "strPassword"), org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
call.setReturnType(org.apache.axis.Constants.XSD_STRING);
String sa = "http://WebXml.com.cn/getWeatherbyCityName";
call.setOperationName(new javax.xml.namespace.QName("http://WebXml.com.cn/", "getWeatherbyCityName"));
call.setUseSOAPAction(true);
call.setSOAPActionURI(sa); 
String result = (String)call.invoke(new Object[]{theCityName});
System.out.println( "result is #" + result.toString() + "#.");
}
catch (Exception e) 
{System.err.println(e.toString());
e.printStackTrace();}
}
  
}

解决方案 »

  1.   

    把你的错误贴上啊!我用axis 调用了 csdn 的 API,还不错!
      

  2.   

    import org.apache.axis.client.Call;
    import org.apache.axis.client.Service;
    import javax.xml.namespace.QName;
    import java.util.Vector;public class SelfInvoke {
      public static void main(String[] args) {
        String theCityName = "南京";
        String soapactionName = "http://WebXml.com.cn/";
        try {
          String endpoint =
              "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";
          Service service = new Service();
          Call call = (Call) service.createCall();
          call.setTargetEndpointAddress(new java.net.URL(endpoint));
          call.addParameter(new QName(soapactionName,
              "theCityName"), org.apache.axis.encoding.XMLType.XSD_STRING,
                            javax.xml.rpc.ParameterMode.IN);
    //call.addParameter(new javax.xml.namespace.QName("http://WebXml.com.cn/", "strPassword"), org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
          call.setReturnType(new QName(soapactionName, "getWeatherbyCityName"), String[].class);//由于返回的是一个数组所以要自定义返回类型
          call.setOperationName(new QName(
              soapactionName, "getWeatherbyCityName"));
          call.setUseSOAPAction(true);
          call.setSOAPActionURI(soapactionName+"getWeatherbyCityName");
          String[] result = (String[]) call.invoke(new Object[] {theCityName});
          for (int i=0;i<result.length;i++)
          {
            System.out.println(result[i]);
          }
        }
        catch (Exception e) {
          System.err.println(e.toString());
          e.printStackTrace();
        }
      }}