我目前正在开发一个将信息输入到我的 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();
}
}
});