1

我正在尝试使用 ksoap2 访问 .NET Web 服务,但我不知道它为什么不起作用。

我已经阅读了很多教程,但仍然无法正常工作。总是得到错误:

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 添加到项目中并添加为库,并且我也设置了使用互联网的权限。

我正在使用安卓 2.2

4

2 回答 2

0

看起来问题出在我的连接上。代码是正确的,但是当我尝试使用 3G 连接时无法正常工作,但是当我决定使用 wi-fi 进行测试时工作正常。

我将添加一个验证和一个反馈,告诉连接到一个 wi-fi,就像已经看到一些应用程序一样。

于 2012-04-26T16:55:22.423 回答
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());
        }
    }
}}

希望这有效.. :)

于 2012-04-25T07:47:44.673 回答