0

我正在尝试从 ASMX Web 服务获取数据。我已经使用了这里的代码并使用了它的 ConvertWaight Web 服务,但它不起作用。

它向我显示错误java.net.SocketTimeoutException: Transport endpoint is not connected。我已经添加了网络权限,但仍然出现此错误。

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

请帮助我,任何示例代码都会非常有帮助。

我的网络服务NSDictionary在 iOS 中接受。像这样...

{
    memberDetails =     {
        DeviceName = "iPhone Simulator";
        DeviceToken = 951fc5a77184ds1a17474256759a34e03d31b708a0358v742df38bb3cc845ds4;
        EmailAddress = "user@org.com";
        IsRemember = True;
        Password = "123456789";
    };
}

所以在android中我试图将HasMap传递给webservice

如果有帮助,这是我的网络服务

POST /service/xxx.asmx HTTP/1.1
Host: www.xxx.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/SelectByEmailAndPassword"

<?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>
    <SelectByEmailAndPassword xmlns="http://tempuri.org/">
      <memberDetails>
        <EmailAddress>string</EmailAddress>
        <Password>string</Password>
        <DeviceToken>string</DeviceToken>
        <DeviceName>string</DeviceName>
        <IsRemember>boolean</IsRemember>
      </memberDetails>
    </SelectByEmailAndPassword>
  </soap:Body>
</soap:Envelope>

这是我调用网络服务的代码

    public String Call(String deviceName, String deviceToken, String emailAddress, String isRemember, String password)
    {
    SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
    PropertyInfo pi=new PropertyInfo();

    //Create a HashMap
    Map <String,String> memberDetails =  new HashMap<String,String>();
    //Put data into the HashMap
    memberDetails.put("DeviceName","android Simulator");
   memberDetails.put("DeviceToken","951fc5a77184ds1a17474256759a34e03d31b708a0358v742df38bb3cc845ds4");
    memberDetails.put("EmailAddress","user@org.com");
    memberDetails.put("IsRemember","True");
    memberDetails.put("Password","123456789");

    pi.setName("memberDetails");
    pi.setValue(memberDetails.toString());
    pi.setType(String.class);
    request.addProperty(pi);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
    SoapEnvelope.VER11);
    envelope.dotNet = true;

    envelope.setOutputSoapObject(request);

    HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
    Object response=null;
    try
    {
    httpTransport.call(SOAP_ACTION, envelope);
    response = envelope.getResponse();
    }
    catch (Exception exception)
    {
    response=exception.toString();
    }
    return response.toString();
    }
    }
4

1 回答 1

0

我检查链接。比我创建 Testconnection 项目。所有 web 服务调用都必须在后台线程上。您可以创建 I 线程并在其中运行您的目标吗?

private void getResult()
{
new Thread(){

    public void run() 
    {
        WebserviceCall com = new WebserviceCall();

        // Initialize variables
        final String weight   = "18000";
        String fromUnit = "Grams";
        String toUnit   = "Kilograms";

        //Call Webservice class method and pass values and get response
        final String aResponse = com.getConvertedWeight("ConvertWeight", weight, fromUnit, toUnit);  

        //Alert message to show webservice response
        button.post(new Runnable() {

            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), weight+" Gram= "+aResponse+" Kilograms",
                        Toast.LENGTH_LONG).show();
            }
        });

        Log.i("AndroidExampleOutput", "----"+aResponse);
    }

    }.start();

}

并且比我的网络服务类在链接中相同;

public class WebserviceCall {

     String namespace = "http://www.webserviceX.NET/";
        private String url = "http://www.webservicex.net/ConvertWeight.asmx";

        String SOAP_ACTION;
        SoapObject request = null, objMessages = null;
        SoapSerializationEnvelope envelope;
        AndroidHttpTransport androidHttpTransport;

        WebserviceCall() {
        }


        /**
         * Set Envelope
         */
        protected void SetEnvelope() {

            try {

                // Creating SOAP envelope          
                envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

                //You can comment that line if your web service is not .NET one.
                envelope.dotNet = true;

                envelope.setOutputSoapObject(request);
                androidHttpTransport = new AndroidHttpTransport(url);
                androidHttpTransport.debug = true;

            } catch (Exception e) {
                System.out.println("Soap Exception---->>>" + e.toString());   
            }
        }

        // MethodName variable is define for which webservice function  will call
        public String getConvertedWeight(String MethodName, String weight,
                String fromUnit, String toUnit)
          {

            try {
                SOAP_ACTION = namespace + MethodName;

                //Adding values to request object
                request = new SoapObject(namespace, MethodName);

                //Adding Double value to request object
                PropertyInfo weightProp =new PropertyInfo();
                weightProp.setName("Weight");
                weightProp.setValue(weight);
                weightProp.setType(double.class);
                request.addProperty(weightProp);

                //Adding String value to request object
                request.addProperty("FromUnit", "" + fromUnit);
                request.addProperty("ToUnit", "" + toUnit);

                SetEnvelope();

                try {

                    //SOAP calling webservice
                    androidHttpTransport.call(SOAP_ACTION, envelope);

                    //Got Webservice response
                    String result = envelope.getResponse().toString();

                    return result;

                } catch (Exception e) {
                    // TODO: handle exception
                    return e.toString();
                }
            } catch (Exception e) {
                // TODO: handle exception
                return e.toString();
            }

        }


}
于 2013-10-10T05:37:33.787 回答