java 调用webservice 求解解决方案

java 调用webservice 求解
本帖最后由 u010784851 于 2014-10-08 08:49:19 编辑
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Vector;

import org.apache.soap.Constants;
import org.apache.soap.Fault;
import org.apache.soap.Header;
import org.apache.soap.SOAPException;
import org.apache.soap.rpc.Call;
import org.apache.soap.rpc.Parameter;
import org.apache.soap.rpc.Response;
import org.apache.soap.util.xml.QName;


public class TestWebService {
   public static String getService() {
  //http://appkey.imjuju.com:8200/api/chatmanage.asmx
       URL url = null;
       try {
           url=new URL("http://appkey.imjuju.com:8200/api/chatmanage.asmx");
       } catch (MalformedURLException mue) {
        return mue.getMessage();
         }
         // This is the main SOAP object
       Call soapCall = new Call();
       // Use SOAP encoding
       soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
       // This is the remote object we're asking for the price
       soapCall.setTargetObjectURI("urn:xmethods-UserLogin");
       // This is the name of the method on the above object
       soapCall.setMethodName("UserLogin");
       Header  header = new Header();
       header.setAttribute(new QName("http://tempuri.org/", "UserLogin"), "");
       soapCall.setHeader(header);
       //soapCall.setOperationName(new QName("", "GetTestQuestions"));
       // We need to send the ISBN number as an input parameter to the method
       Vector soapParams = new Vector();
       // name, type, value, encoding style
      // Parameter isbnParam = new Parameter("userName", String.class, user, null);
       Parameter isbnParam1 = new Parameter("loginname", String.class, "no1", null);
       Parameter isbnParam2 = new Parameter("loginpwd", String.class, "123456", null);
       Parameter isbnParam3 = new Parameter("lat", Float.class, 1.2, null);
       Parameter isbnParam4 = new Parameter("lng", Float.class, 1.20, null);
       soapParams.addElement(isbnParam1);
       soapParams.addElement(isbnParam2);
       soapParams.addElement(isbnParam3);
       soapParams.addElement(isbnParam4);
       soapCall.setParams(soapParams);
       try {
          // Invoke the remote method on the object
          Response soapResponse = soapCall.invoke(url,"");
          // Check to see if there is an error, return "N/A"
          if (soapResponse.generatedFault()) {
              Fault fault = soapResponse.getFault();
             String f = fault.getFaultString();
             return f;
          } else {
             // read result
             Parameter soapResult = soapResponse.getReturnValue ();
             // get a string from the result
             return soapResult.getValue().toString();
          }
       } catch (SOAPException se) {

          return se.getMessage();