Android XStream 对象 Java WebService

发布于 2024-12-22 12:07:53 字数 3169 浏览 0 评论 0原文

我有一个正在运行的 webservice,我想调用 Webservice 的方法,并且应该在移动设备上的 TextView 中显示结果。 Web 服务运行良好,为了进行测试,我可以从 Web 服务获取字符串响应。但我想获得一个 Xstream 对象
我创建了一个 Xstream 对象并且它在本地工作。

当我使用 KSOAP2 调用 webservice 方法获取 xstream 对象时,出现异常 java.lang.classcastexception:org.ksopa2.soapFault

请帮忙。

package com.pxr.tutorial.soap.weather;

import org.ksoap2.SoapEnvelope; 
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;  
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;   
import android.widget.TextView;
import android.widget.Toast;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;


public class Main extends Activity {


private String METHOD_NAME = "getName"; // our webservice method name
private String NAMESPACE = "http://webservice.app"; // Here package
                                                            // name in
                                                            // webservice
                                                            // with reverse
                                                            // order.
private String SOAP_ACTION = "http://webservice.app/getName"; // NAMESPACE
                                                                    // +
                                                                    // method
public TextView t; // name

private static String URL = "http://ip/WS/services/Service?wsdl";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Initialize soap request + add parameters
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    request.addProperty("user", "user");
    request.addProperty("passwort", "passwort");

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

    // Make the soap call.
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    t = (TextView) findViewById(R.id.resultbox);
    // this is the actual part that will call the webservice
    try {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapObject result=(SoapObject)envelope.bodyIn;

        //SoapObject result = (SoapObject) envelope.response();


        if (result != null) {

            XStream xstreams = new XStream(new DomDriver());
            String text = result.getProperty(0).toString();
            Testobjekt tob = (Testobjekt) xstreams.fromXML(text);
            t = (TextView) findViewById(R.id.resultbox);        
            t.setText(tob.getName());
        }

*/
    } catch (Exception e) {
        // TODO Auto-generated catch block
        t.setText("FEHLER");

    }


  }


}

调试时我在这一行收到错误

     SoapObject result=(SoapObject)envelope.bodyIn; 
     SoapObject result = (SoapObject) envelope.response();

I have a running webservice and I want to call the Method of the Webservice and should show the result in a TextView on the mobile. The Webservice is working very well, for testing I can get a string response from webservice. But I want to get an Xstream Object.
I have created an Xstream object and it's working locally.

When I call the webservice method to get the xstream object using KSOAP2 I get the exception java.lang.classcastexception:org.ksopa2.soapFault

Please help.

package com.pxr.tutorial.soap.weather;

import org.ksoap2.SoapEnvelope; 
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;  
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;   
import android.widget.TextView;
import android.widget.Toast;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;


public class Main extends Activity {


private String METHOD_NAME = "getName"; // our webservice method name
private String NAMESPACE = "http://webservice.app"; // Here package
                                                            // name in
                                                            // webservice
                                                            // with reverse
                                                            // order.
private String SOAP_ACTION = "http://webservice.app/getName"; // NAMESPACE
                                                                    // +
                                                                    // method
public TextView t; // name

private static String URL = "http://ip/WS/services/Service?wsdl";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Initialize soap request + add parameters
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    request.addProperty("user", "user");
    request.addProperty("passwort", "passwort");

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

    // Make the soap call.
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    t = (TextView) findViewById(R.id.resultbox);
    // this is the actual part that will call the webservice
    try {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapObject result=(SoapObject)envelope.bodyIn;

        //SoapObject result = (SoapObject) envelope.response();


        if (result != null) {

            XStream xstreams = new XStream(new DomDriver());
            String text = result.getProperty(0).toString();
            Testobjekt tob = (Testobjekt) xstreams.fromXML(text);
            t = (TextView) findViewById(R.id.resultbox);        
            t.setText(tob.getName());
        }

*/
    } catch (Exception e) {
        // TODO Auto-generated catch block
        t.setText("FEHLER");

    }


  }


}

On Debugging I get an error on this line

     SoapObject result=(SoapObject)envelope.bodyIn; 
     SoapObject result = (SoapObject) envelope.response();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文