繁体   English   中英

Ksoap2无法在Android 2.2上序列化异常(使用用户定义的对象)

Ksoap2 cannot serialize exception on Android 2.2 (using user-defined object)

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文繁体   英文版本   中英对照 版本,有任何建议请联系yoyou2525@163.com。

我知道这个问题有很多帖子。 但是它们是在Android 3.0或更高版本上构建的(使用AsyncTask)。 我找不到我的解决方案。 从Web服务获取信息存在问题。 这是我的代码:

package com.example.marport;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

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

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

public class Ithalatfaturalama extends Activity implements OnClickListener {

    EditText KonteynerNo;
    DatePicker DatePicker;
    Button Sorgula;
    TextView TextResult;
    List<String> toBeSentList;
    Iterator<String> it;
    Date toBeSentDate;
    SoapObject request;
    SoapSerializationEnvelope envelope;
    HttpTransportSE aps;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ithalatfaturalama);
        defineVars();
        Sorgula.setOnClickListener(this);
    }

    private void defineVars() {
        KonteynerNo = (EditText)findViewById(R.id.etKonteynerBill);
        DatePicker = (DatePicker)findViewById(R.id.datePicker1);
        Sorgula = (Button)findViewById(R.id.bSorgulaBill);
        TextResult = (TextView)findViewById(R.id.tvResultBill);
        toBeSentList = new ArrayList<String>();
        request = new SoapObject(WSDefinition.NAMESPACE,WSDefinition.REPORTS);
        toBeSentDate = new Date();
        envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    }

    @Override
    public void onClick(View v) {
        String containerInfo = KonteynerNo.getText().toString();
        toBeSentList.add(containerInfo);
        it = toBeSentList.iterator();
        String temp = (String) it.next();
        request.addProperty("ids",temp);

        int Day = DatePicker.getDayOfMonth();
        int Month = DatePicker.getMonth();
        int Year = DatePicker.getYear();

        toBeSentDate.setYear(Year);
        toBeSentDate.setMonth(Month);
        toBeSentDate.setDate(Day);

        request.addProperty("date",toBeSentDate);
        envelope.dotNet = false;
        envelope.setOutputSoapObject(request);

        try {
            aps = new HttpTransportSE(WSDefinition.URL);
            aps.call(WSDefinition.REPORTS_ACTION, envelope);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            @SuppressWarnings("unchecked")
            Vector<SoapObject> result = (Vector<SoapObject>) envelope.getResponse();
            Vector<MarportAgentReport> responseList = new Vector<MarportAgentReport>();

            for (SoapObject obj : result) {

                MarportAgentReport tmp = new MarportAgentReport(obj);
                responseList.add(tmp);
            }

            String resultStr = "";
            for (int i = 0 ; i < responseList.size();i++){
                MarportAgentReport tmp = responseList.get(i);
                resultStr += (i+1) +".Category = "+tmp.getCategory()+"\n";
                resultStr += (i+1) +".Container = "+tmp.getContainer()+"\n";
                resultStr += (i+1) +".InShip= "+tmp.getInShip()+"\n";
                resultStr += (i+1) +".IsoCode = "+tmp.getIsoCode()+"\n";
                resultStr += (i+1) +".Line = "+tmp.getLine()+"\n";
                resultStr += (i+1) +".Muhur = "+tmp.getMuhur()+"\n";
                resultStr += (i+1) +".OutShip = "+tmp.getOutShip()+"\n";
                resultStr += (i+1) +".Position = "+tmp.getPosition()+"\n";
                resultStr += (i+1) +".Service = "+tmp.getService()+"\n";
                resultStr += (i+1) +".Status = "+tmp.getStatus()+"\n";
                resultStr += (i+1) +".Terminal = "+tmp.getTerminal()+"\n";
            }
            TextResult.setText(resultStr);

        } catch (SoapFault e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }
}

这是我的目标:

package com.example.marport;

import java.io.Serializable;
import java.util.Date;
import java.util.Hashtable;

import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;

/**
 * The persistent class for the CONTENT database table.
 * 
 */
public class MarportAgentReport implements KvmSerializable {

    private static final long serialVersionUID = 1L;
    private String container;
    private String isoCode;
    private String category;
    private String status;
    private String line;
    private String service;
    private Date serviceDate;
    private String terminal;
    private String inShip;
    private String outShip;
    private String muhur;
    private String position;


    public MarportAgentReport() {
    }

    public MarportAgentReport(SoapObject obj)
    {
        obj.getPropertyCount();
        obj.getProperty(0);

        this.setCategory(obj);
        this.setInShip(obj);
        this.setContainer(obj);
        this.setIsoCode(obj);
        this.setLine(obj);
        this.setMuhur(obj);
        this.setOutShip(obj);
        this.setPosition(obj);
        this.setService(obj);
        this.setStatus(obj);
        this.setTerminal(obj);
    }

    public String getContainer() {
        return container;
    }


    public void setContainer(SoapObject obj) {
        try {
            this.container = obj.getPropertyAsString("container");
        } catch (Exception e) {
            this.container = "";
        }
    }


    public String getIsoCode() {
        return isoCode;
    }


    public void setIsoCode(SoapObject obj) {
        try {
            this.isoCode = obj.getPropertyAsString("isoCode");
        } catch (Exception e) {
            this.isoCode = "";
        }
    }


    public String getCategory() {
        return category;
    }


    public void setCategory(SoapObject obj) {
        try {
            this.category = obj.getPropertyAsString("category");
        } catch (Exception e) {
            this.category = "";
        }
    }


    public String getStatus() {
        return status;
    }


    public void setStatus(SoapObject obj) {
        try {
            this.status = obj.getPropertyAsString("status");
        } catch (Exception e) {
            this.status = "";
        }
    }


    public String getLine() {
        return line;
    }


    public void setLine(SoapObject obj) {
        try {
            this.line = obj.getPropertyAsString("line");
        } catch (Exception e) {
            this.line = "";
        }
    }


    public String getService() {
        return service;
    }


    public void setService(SoapObject obj) {
        try {
            this.service = obj.getPropertyAsString("service");
        } catch (Exception e) {
            this.service = "";
        }
    }


    public Date getServiceDate() {
        return serviceDate;
    }


    public void setServiceDate(Date serviceDate) {
        this.serviceDate = serviceDate;
    }


    public String getTerminal() {
        return terminal;
    }


    public void setTerminal(SoapObject obj) {
        try {
            this.terminal = obj.getPropertyAsString("terminal");
        } catch (Exception e) {
            this.terminal = "";
        }
    }


    public String getInShip() {
        return inShip;
    }


    public void setInShip(SoapObject obj) {
        try {
            this.inShip = obj.getPropertyAsString("inShip");
        } catch (Exception e) {
            this.inShip = "";
        }
    }


    public String getOutShip() {
        return outShip;
    }


    public void setOutShip(SoapObject obj) {
        try {
            this.outShip = obj.getPropertyAsString("outShip");
        } catch (Exception e) {
            this.outShip = "";
        }
    }


    public String getMuhur() {
        return muhur;
    }


    public void setMuhur(SoapObject obj) {
        try {
            this.muhur = obj.getPropertyAsString("muhur");
        } catch (Exception e) {
            this.muhur = "";
        }
    }


    public String getPosition() {
        return position;
    }


    public void setPosition(SoapObject obj) {
        try {
            this.position = obj.getPropertyAsString("position");
        } catch (Exception e) {
            this.position = "";
        }
    }

    @Override
    public Object getProperty(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int getPropertyCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
        // TODO Auto-generated method stub

    }

    @Override
    public void setProperty(int arg0, Object arg1) {
        // TODO Auto-generated method stub

    }

}

和日志:

09-13 07:13:30.484: E/AndroidRuntime(251): FATAL EXCEPTION: main
09-13 07:13:30.484: E/AndroidRuntime(251): java.lang.RuntimeException: Cannot serialize: Fri Sep 13 07:12:31 GMT+00:00 3912
09-13 07:13:30.484: E/AndroidRuntime(251):  at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:687)
09-13 07:13:30.484: E/AndroidRuntime(251):  at org.ksoap2.serialization.SoapSerializationEnvelope.writeProperty(SoapSerializationEnvelope.java:671)
09-13 07:13:30.484: E/AndroidRuntime(251):  at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:640)
09-13 07:13:30.484: E/AndroidRuntime(251):  at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:624)
09-13 07:13:30.484: E/AndroidRuntime(251):  at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:681)
09-13 07:13:30.484: E/AndroidRuntime(251):  at org.ksoap2.serialization.SoapSerializationEnvelope.writeBody(SoapSerializationEnvelope.java:597)
09-13 07:13:30.484: E/AndroidRuntime(251):  at org.ksoap2.SoapEnvelope.write(SoapEnvelope.java:192)
09-13 07:13:30.484: E/AndroidRuntime(251):  at org.ksoap2.transport.Transport.createRequestData(Transport.java:107)
09-13 07:13:30.484: E/AndroidRuntime(251):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:119)
09-13 07:13:30.484: E/AndroidRuntime(251):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:95)
09-13 07:13:30.484: E/AndroidRuntime(251):  at com.example.marport.Ithalatfaturalama.onClick(Ithalatfaturalama.java:81)
09-13 07:13:30.484: E/AndroidRuntime(251):  at android.view.View.performClick(View.java:2408)
09-13 07:13:30.484: E/AndroidRuntime(251):  at android.view.View$PerformClick.run(View.java:8816)
09-13 07:13:30.484: E/AndroidRuntime(251):  at android.os.Handler.handleCallback(Handler.java:587)
09-13 07:13:30.484: E/AndroidRuntime(251):  at android.os.Handler.dispatchMessage(Handler.java:92)
09-13 07:13:30.484: E/AndroidRuntime(251):  at android.os.Looper.loop(Looper.java:123)
09-13 07:13:30.484: E/AndroidRuntime(251):  at android.app.ActivityThread.main(ActivityThread.java:4627)
09-13 07:13:30.484: E/AndroidRuntime(251):  at java.lang.reflect.Method.invokeNative(Native Method)
09-13 07:13:30.484: E/AndroidRuntime(251):  at java.lang.reflect.Method.invoke(Method.java:521)
09-13 07:13:30.484: E/AndroidRuntime(251):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-13 07:13:30.484: E/AndroidRuntime(251):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-13 07:13:30.484: E/AndroidRuntime(251):  at dalvik.system.NativeStart.main(Native Method)

谢谢。

2 个回复

你需要写

string temp= sorgula.getText().toString();

在try块中从字符串中获取数据。 同样,您需要提及您在服务器端调用的所有字段。

它对我有所帮助。希望它对你也有帮助。 :)

好吧,关于你的对象MarportAgentReport实现KvmSerializable ,首先你需要实现KvmSerializable的覆盖方法,然后运行你的代码,你可能得到一个新的答案! 您可以按照以下文章: http//www.sgoliver.net/blog/?p = 2594

祝好运 !!!

1 ksoap2无法序列化byte []的异常

我正在尝试使用Asp.Net Web服务。 我正在使用kso​​ap2库。 但是我收到“ java.lang.RuntimeException:无法序列化:[B @ 41eb9c40”异常。 我正在尝试通过网络服务发送文件。 我认为问题是由字节数组引起的。 这是我的代码: ...

2 Android中的Ksoap2无法序列化

我在Android中使用kso​​ap2 发送数字列表作为字符串 但是它有错误: java.lang.runtimeexception无法序列化 我搜索此错误的解决方案,但结果未更改 能帮我吗 ...

3 ksoap2自定义序列化响应

我在转换对自定义类的响应时遇到问题:这是调用request的代码: 这是序列化的响应类: 我得到的错误是:SoapObject无法转换为com.myapp ... LoginResponse。 它发生在这一行: 如果我写 而不是前一行,我没有收到错误,但是我想使 ...

5 Ksoap2无法序列化整数数组列表

我正在使用Ksoap2使用soap服务从服务器发送和获取响应。 我发送复杂类型的对象。 为此,我将整个班级序列化。 但是类的一个属性是Integer List。 所以我在请求服务器时收到以下错误 无法序列化[12,11,225,3252,] //这是我的整数数组 所 ...

6 kSoap2序列化错误

我正在使用kSoap2从Android设备连接到WCF服务,但出现序列化错误: 无法序列化:CompositeType:StringValue =测试,BoolValue = true 这是webservice调用的代码: 这是我的CompositeType类的代码: ...

7 错误ksoap2序列化

我尝试制作一个简单的应用程序来提取Web服务的字符串,我已经下载了ksopa2软件包,制作精良的数据和正确的数据(已在Internet教程上进行了测试),但我得到了下面的错误提示,您知道是? 非常感谢你! 应用: } 错误logvcat ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2021 STACKOOM.COM