我正在尝试从ASMX Web服务获取数据。我使用了here中的代码并使用了它的ConvertWaight Web服务,但它无法正常工作。
它显示我错误java.net.SocketTimeoutException: Transport endpoint is not connected
。我添加了网络权限但仍然出现此错误。
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
请帮助我,任何示例代码都会非常有用。
我的网络服务接受iOS中的NSDictionary
。像这样......
{
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();
}
}
答案 0 :(得分:0)
我检查link。比我创建Testconnection项目。所有webservice调用必须在后台线程上。你能创建我的线程并在其中运行你的目标吗?
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();
}
}
}