【问题标题】:Async Background thread WebServices异步后台线程 WebServices
【发布时间】:2014-05-26 10:10:34
【问题描述】:

我收到一个错误,告诉我它在运行时无法序列化。

一个异常正在捕获该行的错误

androidHttpTransport.call(SOAP_UPDATE, 信封);

错误是:java.lang.RuntimeException: Cannot serialize: sprint.telematics.sprinttelematicstracking.RequestInfo@42df66d8

我正在使用 WebServices 并尝试在后台 AsyncThread 上运行一个,但一直收到错误消息。 ASyncThread 是 MainActivity 的一个 InnerClass

public class JobStatus extends AsyncTask<SoapObject,Object, SoapObject> 
{
    String sessionID;
    Globals globals;

    int jobStatus = 0, requestsSent = 0;
    Context context;
    CreateJobStatus createJobStatus;
    Bundle extras = getIntent().getExtras();

    @Override
    protected SoapObject doInBackground(SoapObject... request) 
    {
        // TODO Auto-generated method stub

        for(int i=10;i<100;i +=10)
        {
            try
            {
                    requestSent = requestSent + 1;

                    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

                    envelope.setOutputSoapObject(request[0]);
                    envelope.dotNet = true;

                    try
                    {
                        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL_UPDATE);

                        //this is the actual part that will call the web service
                        androidHttpTransport.call(SOAP_UPDATE, envelope);

                        //Get the SoapResult from the envelope body
                        SoapObject result = (SoapObject)envelope.bodyIn;
                        if(result != null)
                        {
                            jobStatus = jobStatus + 1;
                            TextView jobStatus_ = (TextView) findViewById(R.id.JobsStatus);
                            jobStatus_.setText(jobs + " of " + requestsSent);
                        } 
                } 
                catch(Exception ex)
                {

                    ex.printStackTrace();
                }
                }
                publishProgress(i);
            }
            catch(InterruptedException ex)
            {
                ex.printStackTrace();
            }
        }
        return null;
    }
}

【问题讨论】:

  • 因为你试图从后台线程访问 UI 元素并且不使用 Thread.sleep() 方法。\
  • 我应该摆脱 Thread.sleep() 吗?

标签: android web-services android-asynctask


【解决方案1】:

删除

TextView jobStatus_ = (TextView) findViewById(R.id.JobsStatus);
jobStatus_.setText(jobs + " of " + requestsSent);

来自

做背景

(您不能从后台线程访问 UI 元素) 相反,请将它们放在 AsyncTask 的 onPostExecute 方法中。

【讨论】:

  • 错误来自这一行,虽然 androidHttpTransport.call(SOAP_UPDATE, envelope);
  • 那么请将您遇到的错误添加到您的问题中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多