2012-04-25 105 views
1

我試圖訪問一個.NET webservice使用kso​​ap2,但我不知道它爲什麼不工作。如何連接到.net web服務使用kso​​ap2 android

我已經閱讀了很多教程,但仍然無法正常工作。 總是出現錯誤:

at org.kxml2.io.KXmlParser.exception(KXmlParser.java:273) 
at org.kxml2.io.KXmlParser.nextTag(KXmlParser.java:1413) 
at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:126) 
at org.ksoap2.transport.Transport.parseResponse(Transport.java:63) 
at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:100) 
at br.android.listgame.Activitys.ListSimpleActivity.onCreate(ListSimpleActivity.java:108) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1660) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1721) 
at android.app.ActivityThread.access$1500(ActivityThread.java:124) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:130) 
at android.app.ActivityThread.main(ActivityThread.java:3844) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:507) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
at dalvik.system.NativeStart.main(Native Method) 

我已經在使用的soapUI測試web服務和它的正常工作。

SoapObject soap = new SoapObject(NAMESPACE, OPERATION); 
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

    soap.addProperty("nome", "artur"); 
    envelope.dotNet = true; 
    envelope.setOutputSoapObject(soap); 

    HttpTransportSE httpTransport = new HttpTransportSE(URL); 
    try { 

     httpTransport.call(SOAP_ACTION, envelope); 
     resposta = envelope.getResponse().toString(); 

    } catch (IOException ioex) { 
     ioex.printStackTrace(); 
    } catch (XmlPullParserException e) { 
     e.printStackTrace(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     // TODO: handle exception 
    } 

我已經將.jar添加到項目並添加爲庫,並且我也設置了使用互聯網的權限。

我使用的是Android 2.2的

+0

請顯示引發異常的行。 – 2012-04-25 02:22:35

+0

httpTransport.call(SOAP_ACTION,envelope); – FilipeFaria 2012-04-25 02:32:07

回答

0

我想你在下面的行有問題...

`resposta = envelope.getResponse().toString();` 

試試這個代碼...它的工作對我來說...

public class FinalWebServiceDemoActivity extends Activity { 

// some parameters regarding your web-service to be invoked 
private static final String SOAP_ACTION = "http://tempuri.org/WebServiceMethod"; 
private static final String METHOD_NAME = "WebServiceMethod"; 
private static final String NAMESPACE = "http://tempuri.org/"; 
private static final String URL = "http://10.0.2.2:2256/WebService.asmx"; 

TextView tv; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    tv=(TextView)findViewById(R.id.text1); 
    call(); 
} 

public void call() 
{ 
    try { 
     SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
     request.addProperty("message","Hello"); 

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

     HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
     androidHttpTransport.call(SOAP_ACTION, envelope); 

     SoapPrimitive result = (SoapPrimitive)envelope.getResponse(); 
     String strRes = result.toString(); 

     tv.setText(strRes); 
    } catch (Exception e) { 
     tv.setText("Exception..."); 
     Log.i("exception", e.toString()); 
     StackTraceElement elements[] = e.getStackTrace(); 
     for (int i = 0, n = elements.length; i < n; i++) {  
      Log.i("File", elements[i].getFileName()); 
      Log.i("Line", String.valueOf(elements[i].getLineNumber())); 
      Log.i("Method", elements[i].getMethodName()); 
     } 
    } 
}} 

希望這個工程.. :)

0

看起來像問題是我的連接。代碼是正確的,但是當我嘗試使用3G連接時無法正常工作,但是當我決定使用wi-fi進行測試時工作正常。

我會添加一個驗證和一個反饋,告訴連接到一個Wi-Fi,就像已經看到了一些應用程序。

相關問題