现在我已经使用 SOAP 方法来使用来自 android eclipse 的 web 服务。
我需要从 edittext 获取输入并显示正确的值,这意味着调用 Web 服务并返回结果。
模拟器运行后,显示为“错误: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;
}
}
日志猫
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);
} }
非常感谢!!..