Hello stackoveflow团队,
我遇到了一个奇怪的错误。我使用ksop2拨打http://www.webserviceX.NET肥皂。当我使用avm或智能手机与Android 2.3.3,以下代码完美。当我在版本4的Android智能手机上运行此代码时,我收到此错误:
E / ActivityRecord(212):发生sendActivityPerformanceInfo异常:java.io.IOException:传输端点未连接
以下是代码:
soapObject = new SoapObject(namespace, "GetCitiesByCountry");
soapObject.addProperty("CountryName", countryIn);
envelope.setOutputSoapObject(soapObject);
envelope.bodyOut = soapObject;
envelope.dotNet = true;
httpTransportSE = new HttpTransportSE("http://www.webservicex.net/globalweather.asmx");
httpTransportSE.debug = true;
Log.d(TAG, "********** http - Call ***********");
httpTransportSE.call("http://www.webserviceX.NET/GetCitiesByCountry", envelope);
httpTransportSE.call(..)发生错误 也许你可以帮助我。
答案 0 :(得分:2)
假设您已检查了所有这些步骤(由于某些原因,其中许多步骤未返回corect异常名称)
我别无选择,只能断定您正在尝试从主线程建立连接,从而生成自API 11(3.0)以来添加的NetworkOnMainThreadException类型的异常
解决这个问题的方法是你必须从第二个线程运行你的调用并以某种策略获得结果“观察者”
这是一个示例代码,如果您想实现此方法
SOAP连接器类
public class SOAPConnector extends Observable {
private static boolean isRunAction=false;
private static String ACTION = "";
private static String METHOD_NAME = "";
private static String NAMESPACE = "";
private static String URL_hprotocol = "";
private static String URL_host = "";
private static String URL_service = "";
private static String outData = "";
private static String inData = "";
private final static int HTTPS_PORT = 443;
private final static int HTTPS_TIMEOUT = 5000;
private static boolean isHttps=false;
public SOAPConnector(String action,
String method,
String namespace,
String hprotocol,
String host,
String service){
if(!isRunAction){
ACTION = action;
METHOD_NAME = method;
NAMESPACE = namespace;
URL_hprotocol = hprotocol;
URL_host = host;
URL_service = service;
}
}
public void sendSoapData(String data,boolean https){
if(!isRunAction){
isHttps = https;
outData = data;
Thread threadsoap = new Thread(runSendSoap);
threadsoap.start();
}
}
private Runnable runSendSoap = new Runnable(){
public void run(){
launchSendSOAP();
handlerSendSOAP.sendEmptyMessage(0);
}
private Handler handlerSendSOAP = new Handler() {
public void handleMessage(Message msg) {}
};
};
private void launchSendSOAP(){
isRunAction=true;
try {
SoapObject client = new SoapObject(NAMESPACE,METHOD_NAME);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String tmpDate = sdf.format(new Date(System.currentTimeMillis()));
String tmpData = outData;
client.addProperty("dataFIELD",(String)tmpData);
client.addProperty("dateFIELD",(String)tmpDate);
client.addProperty("codeFIELD","NULL");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.dotNet=false;
envelope.setOutputSoapObject(client);
if(isHttps){
HttpsTransportSE androidHttpsTransport = new HttpsTransportSE(URL_host, HTTPS_PORT, URL_service,HTTPS_TIMEOUT);
androidHttpsTransport.call(ACTION,envelope);
}else{
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL_hprotocol+URL_host+URL_service);
androidHttpTransport.call(ACTION,envelope);
}
Object result = envelope.getResponse();
inData=result.toString();
} catch (Exception e) {
inData="ERROR";
}
isRunAction=false;
setChanged();
notifyObservers(inData);
}
}
活动电话
private SOAPConnector soapConnector = null;
private ConnectorObserver conObserver =null;
private boolean isActionSoap=false;
private void setServiceObserver(){
soapConnector = new SOAPConnector(SOAP_ACTION_dummy,
METHOD_NAME_dummy,
NAMESPACE_dummy,
URL_hprotocol,
URL_host,
URL_service);
}
private void soapSendProcess(){
if(!isActionSoap){
isActionSoap=true;
if(soapConnector==null){
setServiceObserver();
}
conObserver = new ConnectorObserver();
soapConnector.deleteObservers();
soapConnector.addObserver((Observer)conObserver);
soapConnector.sendSoapData("TEST",false);
}
public class ConnectorObserver implements Observer{
@Override
public void update(Observable observable, Object data){
String resultData = (String)data;
if(resultData.equals("ERROR")){
//TODO fail action
}else{
//TODO parsing action
}
soapConnector.deleteObservers();
isActionSoap=false;
}
}
此代码已经过测试,使用2.2~4.03终端
和ksoap2-android-assembly-3.0.0-jar-RC.4-with-dependencies.jar库。 < / p>