0

我目前正在开发一个将信息输入到我的 JobStatus 表中的应用程序。本质上,这个 ASyncTask 应该在单击 mSign 按钮时运行。但是,我不断收到相同的错误。我猜这可能与我在调用 AsyncTask 时用作参数有关。

根据经验,是否有人使用 AsyncTask 将 Android 应用程序中的信息输入 SQL Server 数据库?在这样的 HTTPPost 实例中,ASyncTask 使用的“可接受”参数是什么?

有什么我在这里想念的吗?我之前已经实现了 HttpGet,但我对整个 HttpPost 和 AsyncTask 还是很陌生,所以任何帮助都将不胜感激。

再次感谢大家。:)

05-31 14:11:51.029: E/WindowManager(9545): Activity com.signonglass.CaptureSignature has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4178f590 that was originally added here
05-31 14:11:51.029: E/WindowManager(9545): android.view.WindowLeaked: Activity com.signonglass.CaptureSignature has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4178f590 that was originally added here
05-31 14:11:51.029: E/WindowManager(9545):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:363)
05-31 14:11:51.029: E/WindowManager(9545):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:268)
05-31 14:11:51.029: E/WindowManager(9545):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:216)
05-31 14:11:51.029: E/WindowManager(9545):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:141)
05-31 14:11:51.029: E/WindowManager(9545):  at android.view.Window$LocalWindowManager.addView(Window.java:537)
05-31 14:11:51.029: E/WindowManager(9545):  at android.app.Dialog.show(Dialog.java:278)
05-31 14:11:51.029: E/WindowManager(9545):  at com.signonglass.CaptureSignature$updateJobStatus.onPreExecute(CaptureSignature.java:210)
05-31 14:11:51.029: E/WindowManager(9545):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
05-31 14:11:51.029: E/WindowManager(9545):  at android.os.AsyncTask.execute(AsyncTask.java:511)
05-31 14:11:51.029: E/WindowManager(9545):  at com.signonglass.CaptureSignature$2.onClick(CaptureSignature.java:169)
05-31 14:11:51.029: E/WindowManager(9545):  at android.view.View.performClick(View.java:3517)
05-31 14:11:51.029: E/WindowManager(9545):  at android.view.View$PerformClick.run(View.java:14155)
05-31 14:11:51.029: E/WindowManager(9545):  at android.os.Handler.handleCallback(Handler.java:605)
05-31 14:11:51.029: E/WindowManager(9545):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-31 14:11:51.029: E/WindowManager(9545):  at android.os.Looper.loop(Looper.java:154)
05-31 14:11:51.029: E/WindowManager(9545):  at android.app.ActivityThread.main(ActivityThread.java:4624)
05-31 14:11:51.029: E/WindowManager(9545):  at java.lang.reflect.Method.invokeNative(Native Method)
05-31 14:11:51.029: E/WindowManager(9545):  at java.lang.reflect.Method.invoke(Method.java:511)
05-31 14:11:51.029: E/WindowManager(9545):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
05-31 14:11:51.029: E/WindowManager(9545):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
05-31 14:11:51.029: E/WindowManager(9545):  at dalvik.system.NativeStart.main(Native Method)

以下是关于我的 AsyncTask 的一些关键事项:-

private final static String jobURI = "http://192.168.0.105:8095/CentralMonitoring/CentralMonitor.svc/addJobStatus/";

WCF接口:

 [OperationContract]
        [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "addJobStatus")]
        wsSQLResult AddJobStatus(Stream JSONdataStream);

WCF 服务:(我已经使用 Fiddler 对此进行了测试,并且工作正常)。

public wsSQLResult AddJobStatus(Stream JSONdataStream)
        {
            wsSQLResult result = new wsSQLResult();
            try
            {
                // Read in our Stream into a string...
                StreamReader reader = new StreamReader(JSONdataStream);
                string JSONdata = reader.ReadToEnd();

                // ..then convert the string into a single "wsCustomer" record.
                JavaScriptSerializer jss = new JavaScriptSerializer();
                JobStatusObj jso = jss.Deserialize<JobStatusObj>(JSONdata);

                if (jso == null)
                {
                    result.WasSuccessful = 0;
                    result.Exception = "Unable to deserialize the JSON data.";
                }
                else
                {
                    CentralMonitoringDataContext cdc = new CentralMonitoringDataContext();
                    JobStatus js = new JobStatus()
                    {
                        JobStatusID = jso.JobStatusID,
                        JobType = jso.jobType,
                        QLSJobID = jso.qlsJobID,
                        DateComplete = DateTime.Parse(jso.dateComplete),
                        TimeComplete = DateTime.Parse(jso.timeComplete),
                        Latitude = Convert.ToDecimal(jso.latitude),
                        Longitude = Convert.ToDecimal(jso.longitude),
                        RecipientName = jso.recipientName
                    };

                    if (!cdc.JobStatus.Any(cj => cj.QLSJobID == jso.qlsJobID))
                    {
                        cdc.JobStatus.InsertOnSubmit(js);
                        cdc.SubmitChanges();

                        result.WasSuccessful = 1;
                        result.Exception = "";
                    }

                }
            }
            catch (Exception ex)
            {
                result.WasSuccessful = 0;
                result.Exception = ex.Message;
            }
            return result;
        }  
    }

最后,这里是 AsyncTask 的代码:

public class addJobStatus extends AsyncTask<JobStatus, Void, Void>
    {
        private ProgressDialog progressDialog = new ProgressDialog(CaptureSignature.this);
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        InputStream inputStream = null;
        StringBuilder builder;


        protected void onPreExecute()
        {
            progressDialog.setMessage("Updating " + uniqueId +"...");
            progressDialog.show();
            progressDialog.setOnCancelListener(new OnCancelListener()
            {
                public void onCancel(DialogInterface arg0)
                {
                    addJobStatus.this.cancel(true);

                }
            });     
        }

        @Override
        protected Void doInBackground(JobStatus... arg0)
        {
            try
            {
                JSONObject js = new JSONObject();
                js.put("jobType", cObj.getJobType());
                js.put("qlsJobID", cObj.getConsignmentID());
                js.put("dateComplete", sdf.format(currentTime));
                js.put("timeComplete", sdf.format(currentTime));
                js.put("latitude", location.getLatitude());
                js.put("longitude", location.getLongitude());
                js.put("recipientName", yourName.getText().toString());

                JSONArray jsArray = new JSONArray();
                jsArray.put(js);

                nameValuePairs.add(new BasicNameValuePair("addJobStatus", jsArray.toString()));
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost request = new HttpPost(jobURI);
                request.addHeader("Content-Type", "application/x-www-form-urlencoded");

                //StringEntity se = new StringEntity(jsArray.toString(), "UTF-8");

                request.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
                HttpResponse response = httpClient.execute(request);
                HttpEntity entity = response.getEntity();
                inputStream = entity.getContent();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            return null;
        }

        protected void onPostExecute(Void unused)
        {
            progressDialog.dismiss();
            Toast.makeText(getApplicationContext(), "Value updated", Toast.LENGTH_SHORT).show();
        }



    }

我打算此时在这里调用 AsyncTask:-

mGetSign.setOnClickListener(new OnClickListener() 
        {        
            public void onClick(View v) 
            {
                Log.v("log_tag", "Panel Saved");
                boolean error = captureSignature();
                if(!error)
                {
                    //initialise new jobStatus object here.
                    new addJobStatus().execute(js);
                    mView.setDrawingCacheEnabled(true);
                    mSignature.save(mView);
                    Bundle b = new Bundle();
                    b.putString("status", "done");
                    Intent intent = new Intent();
                    intent.putExtras(b);
                    setResult(RESULT_OK,intent);
                    finish(); 
                }
            }
        });
4

2 回答 2

1

导致问题的不是参数。它根本与 HTTP 无关 :-)

问题是,在您开始异步任务后,您正在通过finish()调用完成您的活动。

这导致异步任务progressDialog.show()调用失败,因为没有 Activity 可以显示对话框!

如果您必须在 Async 任务之后完成您的活动,请在您关闭对话框后在 Async 任务的 onPostExecute() 方法中执行此操作。这将确保没有像您现在遇到的那样的 Window 泄漏错误。

于 2013-05-31T05:18:34.310 回答
0

在@Anup 的帮助下,我解决了这个问题。我相信,这里的问题,首先是: -

  1. WCF 服务中没有数组声明。所以我在我的 AsyncTask 中取出了 JSONArray。

  2. 我从 URI 中取出了“/”。

  3. 正如向我建议的那样,@Anup 提到的“finish()”方法。

条目现在像明天一样进入数据库。:)

即使这已经排序,我仍然先给@Anup 提个醒。:)

于 2013-05-31T06:59:01.647 回答