Android XStream 对象 Java WebService
我有一个正在运行的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论