2014-01-23 38 views
1

我有這個位置的web服務:Android - 當我在設備上運行它時無法訪問webservice,但使用AVD它正在工作。爲什麼?

<wsdlsoap:address location="http://app.example.com:8080/WSTest/services/Hello"/>

而且我想在我的Android應用程序訪問此WebService。我這樣做:

private class ConnectTask extends AsyncTask<String, Void, String> { 

     @Override 
     protected String doInBackground(String... params) { 

      private final String NAMESPACE = "http://ws.audiomobil.com"; 
      private final String URL = "http://app.example.com:8080/WSTest/services/Hello?wsdl"; 
      private final String SOAP_ACTION = "http://ws.audiomobil.com/hello"; 
      private final String METHOD_NAME = "hello"; 

      try { 
       SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

       String firstName = "Android"; 
       String lastName = "Program"; 
/* 
       // Pass value for fname variable of the web service 
       PropertyInfo fnameProp = new PropertyInfo(); 
       fnameProp.setName("fname");   // Define the variable name in the web service method 
       fnameProp.setValue(firstName);  // Define value for fname variable 
       fnameProp.setType(String.class); // Define the type of thevariable 
       request.addProperty(fnameProp);  // Pass properties to thevariable 

       // Pass value for lname variable of the web service 
       PropertyInfo lnameProp = new PropertyInfo(); 
       lnameProp.setName("lname"); 
       lnameProp.setValue(lastName); 
       lnameProp.setType(String.class); 
       request.addProperty(lnameProp); 
*/ 
       SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
       envelope.dotNet = true; 
       envelope.setOutputSoapObject(request); 
       HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,60000); 

       try { 
        androidHttpTransport.call(SOAP_ACTION, envelope); 
        SoapPrimitive response = (SoapPrimitive) envelope.getResponse(); 
        String s = response.toString(); 
        System.out.println("WS: " + s); 
       } catch (Exception e) { 
        System.out.println(e); 
        e.printStackTrace(); 
       } 
      } catch (Exception e) { 
       System.out.println(e); 
       e.printStackTrace(); 
      } 
      return null; 

     } 
    } 

++++++編輯 我的WebService類是這樣的:

public class Hello { 
    public String hello(String fname, String lname){ 
     return "Hello"; 
    } 
} 

+++++++

當我運行這對我的AVD模擬器 Android 4.0.3然後它的工作正常。它可以訪問web服務,並獲得響應。

但是,當我在我的設備上運行它版本4.0.3,那麼它不起作用。它在這一點停止:androidHttpTransport.call(SOAP_ACTION, envelope);

它拋出一個XMLPullParserException。繼承人logcat:

01-23 11:24:36.959: I/System.out(8172): org.xmlpull.v1.XmlPullParserException: 

unterminated entity ref (position:TEXT @1:86 in [email protected]) 

我真的不知道如何發生這種情況。任何人都可以幫我嗎?

+0

是移動互聯網上的設備,當你運行該應用程序? – SudoRahul

+0

yes intnernet已打開。 –

回答

0

我想你錯過了xml版本標籤。嘗試調用

androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); 
androidHttpTransport.call(SOAP_ACTION, envelope); 

示例代碼之前設置XML版本標記

public static String webServiceForScriptTimeLimit() { 
     String scriptTimeLimitResponse = null; 
     try { 
      SoapObject request = new SoapObject(NAMESPACE, 
        SCRIPT_TIMELIMIT_METHOD_NAME); 
      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
        SoapEnvelope.VER11); 
      envelope.dotNet = true; 
      envelope.implicitTypes = true; 
      envelope.enc = SoapSerializationEnvelope.ENC2003; 
      envelope.xsd = SoapEnvelope.XSD; 
      envelope.xsi = SoapEnvelope.XSI; 
      envelope.setOutputSoapObject(request); 
      envelope.setAddAdornments(false); 
      HttpTransportSE ht = new HttpTransportSE(SOAP_ADDRESS); 
      ht.debug = true; 
      ht.call(SOAP_ACTION_TIME_LIMIT, envelope); 
      final SoapPrimitive response = (SoapPrimitive) envelope 
        .getResponse(); 
      scriptTimeLimitResponse = response.toString(); 

     } 

     catch (Exception ex) { 
      FileLog.logInfo(
        "Exception ---> WS_ERROR ---> WebServiceUtility: webServiceForScriptTimeLimit() - " 
          + ex.toString(), 0); 
     } 
     return scriptTimeLimitResponse; 
    } 
+0

沒有相同的例外:(我只是不明白爲什麼它的工作在AVD和設備上不是更多 –

+0

我認爲你傳遞的xml格式不符合web服務參數的格式可能錯誤 –

+0

但它爲什麼工作在AVD呢? –

相關問題