1

你好 stackoveflow 团队,

我遇到了一个奇怪的错误。我使用 ksop2 对http://www.webserviceX.NET进行肥皂调用。当我使用 android 2.3.3 的 avm 或智能手机时,以下代码可以完美运行。当我在版本 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(..) 也许你可以帮助我。

4

1 回答 1

2

假设您已经检查了所有这些步骤(并且由于某种原因,其中许多没有返回正确的异常名称)

  • 你有一个活跃的连接。
  • 您拥有 Internet 权限。
  • 用于 2.x 终端的代码与 4.x 终端使用的代码相同。
  • 您最近使用作为 soapUI 的应用程序或其他终端测试了您的服务链接类型。
  • 您确定您的代码当前在其他终端上正确运行。

我别无选择,只能得出结论,您正在尝试从主线程建立连接,从而生成自 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 库进行了测试。

于 2012-12-31T01:41:08.467 回答