gpt4 book ai didi

java - Android 无法序列化错误

转载 作者:行者123 更新时间:2023-12-01 12:41:39 24 4
gpt4 key购买 nike

我正在开发一个使用 android 调用 Web 服务的项目。我使用 ksoap2 来实现这一点。我创建了一个自己的数据类型(只是为了尝试),其中包含两个字符串变量。是这样的

public class MyType {
String fName;
String lName;

public MyType(String s1,String s2){
fName = s1;
lName = s2;
}
}

我在两端创建了这个数据类型。(Web服务端和Android应用程序端)。我编写了一个程序来调用 Web 服务,然后使用我的数据类型连接给定的字符串。

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.os.AsyncTask;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

public final static String URL = "http://192.168.69.1:8080/WebApplication4/MyWebService?wsdl";
public static final String NAMESPACE = "http://mywebservice.android.com/";
public static final String SOAP_ACTION_PREFIX = "/";
private static final String METHOD = "objectMethod";
private TextView textView;

MyType mt = new MyType("Upul","Tharanga");

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.test);
AsyncTaskRunner runner = new AsyncTaskRunner(mt);
runner.execute();
}

private class AsyncTaskRunner extends AsyncTask<Integer, String, String> {

private String resp;
MyType a;

public AsyncTaskRunner(MyType a){
this.a = a;
}

@Override
protected String doInBackground(Integer... params) {
publishProgress("Loading contents..."); // Calls onProgressUpdate()
try {
// SoapEnvelop.VER11 is SOAP Version 1.1 constant
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
SoapObject request = new SoapObject(NAMESPACE, METHOD);

PropertyInfo pi1=new PropertyInfo();
pi1.setType(String.class);
pi1.setName("parameter");
pi1.setValue(a);
request.addProperty(pi1);

envelope.bodyOut = request;
HttpTransportSE transport = new HttpTransportSE(URL);
try {
transport.call(NAMESPACE + SOAP_ACTION_PREFIX + METHOD, envelope);
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
//bodyIn is the body object received with this envelope
if (envelope.bodyIn != null) {
//getProperty() Returns a specific property at a certain index.
//SoapPrimitive resultSOAP = (SoapPrimitive) ((SoapObject) envelope.bodyIn).getProperty(0);
//Object resultSOAP = (SoapPrimitive)((SoapObject) envelope.bodyIn).getProperty(0);
Object resultSOAP = (SoapPrimitive)((SoapObject) envelope.bodyIn).getProperty(0);
resp=resultSOAP.toString();
}
} catch (Exception e) {
e.printStackTrace();
resp = e.getMessage();
}
return resp;
}

/**
*
* @see android.os.AsyncTask#onPostExecute(java.lang.Object)
*/
@Override
protected void onPostExecute(String result) {
// execution of result of Long time consuming operation
// In this example it is the return value from the web service
textView.setText(result);
}

/**
*
* @see android.os.AsyncTask#onPreExecute()
*/
@Override
protected void onPreExecute() {
// Things to be done before execution of long running operation. For
// example showing ProgessDialog
}
/**
*
* @see android.os.AsyncTask#onProgressUpdate(Progress[])
*/
@Override
protected void onProgressUpdate(String... text) {
textView.setText(text[0]);
// Things to be done while execution of long running operation is in
// progress. For example updating ProgessDialog
}
}


}

我的 Web 服务所做的是将 MyType 参数作为输入,连接这两个给定的字符串并返回连接后的字符串。当我运行 android 应用程序时,我收到一个错误(我认为是运行时错误),提示无法序列化 MyType。有解决问题的建议吗?

最佳答案

尝试实现可序列化

public class MyType implements Serializable {
String fName;
String lName;

public MyType(String s1,String s2){
fName = s1;
lName = s2;
}
}

关于java - Android 无法序列化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25042692/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com