现在我使用SOAP方法从android eclipse中使用web服务。
我需要从edittext获取输入并显示正确的值,这意味着调用Web服务并返回结果。
模拟器运行后,它显示为“ERROR:java.lang.RuntimeException:无法序列化0004”。如何克服此错误。
这是我的代码,请找
screen.java
public class X_classActivity extends Activity
{
private EditText edit;
private Button btn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edit = (EditText) findViewById(R.id.editText1);
btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
fun();
}
});
}
private void fun()
{
//getSOAPResponse() method returns SOAP Response
SoapObject soapResponseObject = getSOAPResponse(edit.getText());
}
public SoapObject getSOAPResponse(GetChars search)
{
SoapObject soapObject = new SoapObject("http://tempuri.org/", "GetName");
soapObject.addProperty("str", search);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.dotNet = true;
envelope.setOutputSoapObject(soapObject);
HttpTransportSE androidHttpTransport = new HttpTransportSE("http://122.248.240.105:234/Service1.asmx?WSDL");
try
{
androidHttpTransport.call("http://tempuri.org/GetName", envelope);//call the eb service Method
Object result = envelope.getResponse();
System.out.println(" " + result.toString());
((TextView) findViewById (R.id.textView2)).setText(""+result.toString());
}
catch (Exception E)
{
E.printStackTrace();
((TextView) findViewById (R.id.textView2)).setText("ERROR:" + E.getClass().getName() + ":" + E.getMessage());
}
return soapObject;
}
}
logcat的
06-01 18:39:56.020: W/KeyCharacterMap(932): No keyboard for id 0
06-01 18:39:56.020: W/KeyCharacterMap(932): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
06-01 18:40:00.510: W/System.err(932): java.lang.RuntimeException: Cannot serialize: 0004
06-01 18:40:00.520: W/System.err(932): at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:661)
06-01 18:40:00.520: W/System.err(932): at org.ksoap2.serialization.SoapSerializationEnvelope.writeProperty(SoapSerializationEnvelope.java:645)
06-01 18:40:00.520: W/System.err(932): at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:614)
06-01 18:40:00.520: W/System.err(932): at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:598)
06-01 18:40:00.520: W/System.err(932): at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:655)
06-01 18:40:00.530: W/System.err(932): at org.ksoap2.serialization.SoapSerializationEnvelope.writeBody(SoapSerializationEnvelope.java:579)
06-01 18:40:00.530: W/System.err(932): at org.ksoap2.SoapEnvelope.write(SoapEnvelope.java:192)
06-01 18:40:00.530: W/System.err(932): at org.ksoap2.transport.Transport.createRequestData(Transport.java:74)
06-01 18:40:00.530: W/System.err(932): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:58)
06-01 18:40:00.530: W/System.err(932): at com.x.x.x.X_classActivity.getSOAPResponse(X_classActivity.java:57)
06-01 18:40:00.530: W/System.err(932): at com.x.x.x.X_classActivity.fun(X_classActivity.java:45)
06-01 18:40:00.530: W/System.err(932): at com.x.x.x.X_classActivity.access$0(X_classActivity.java:41)
06-01 18:40:00.530: W/System.err(932): at com.x.x.x.X_classActivity$1.onClick(X_classActivity.java:36)
06-01 18:40:00.530: W/System.err(932): at android.view.View.performClick(View.java:2408)
06-01 18:40:00.530: W/System.err(932): at android.view.View$PerformClick.run(View.java:8816)
06-01 18:40:00.530: W/System.err(932): at android.os.Handler.handleCallback(Handler.java:587)
06-01 18:40:00.530: W/System.err(932): at android.os.Handler.dispatchMessage(Handler.java:92)
06-01 18:40:00.530: W/System.err(932): at android.os.Looper.loop(Looper.java:123)
06-01 18:40:00.530: W/System.err(932): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-01 18:40:00.530: W/System.err(932): at java.lang.reflect.Method.invokeNative(Native Method)
06-01 18:40:00.530: W/System.err(932): at java.lang.reflect.Method.invoke(Method.java:521)
06-01 18:40:00.540: W/System.err(932): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-01 18:40:00.540: W/System.err(932): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-01 18:40:00.540: W/System.err(932): at dalvik.system.NativeStart.main(Native Method)
网络方法
public class GetName {
public String GetName(String str){
return(str);
} }
非常感谢!! ..
答案 0 :(得分:0)
我认为你在这一行做错了什么,
androidHttpTransport.call("http://tempuri.org/GetName", envelope);//call the eb service Method
我认为第一个论点是错误的。请参阅以下代码,它对我来说很好。还请导出您构建项目的依赖项。有时它会帮助你。
private String METHOD_NAME = "";
// our webservice method name
private String NAMESPACE = "http://ws.travel_ceylon.web.org";
// Here package name in webservice with reverse order.
private String SOAP_ACTION = NAMESPACE + METHOD_NAME;
// NAMESPACE + method name
private static final String URL = "http://192.168.177.130:8080/Travel_Ceylon_Central_Web_Service/services/Travel_Ceylon_Web_Service?wsdl";
// you must use ipaddress here, don’t use Hostname or localhost
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String city = "Matara";
String im = "Galle Face";
METHOD_NAME = "getLongitude_City";
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("city", city);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
Object result = envelope.getResponse();
((TextView) findViewById(R.id.txtCityLongitude)).setText(city
+ " Longitude is : " + result.toString());
} catch (Exception E) {
E.printStackTrace();
((TextView) findViewById(R.id.txtCityLongitude)).setText("ERROR:"
+ E.getClass().getName() + ":" + E.getMessage());
}