我的 AsyncCode 和服务代码运行良好,但是当我将它们合并在一起时,它会使我的应用程序的 GUI 崩溃,代码设法写入数据库。我是android开发的新手,所以我不太清楚它为什么会崩溃。
这是我的方法代码
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Log.e("<<myServiceRunner-onStart>>", "I am alive-3!");
Thread triggerService = new Thread(new Runnable(){
String msg = "message";
long startingTime = System.currentTimeMillis();
long tics = 0;
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; (i < 120) & isRunning; i++) {
try {
tics = System.currentTimeMillis() - startingTime;
Intent myFilterResponse = new Intent("liren.action.GOSERVICE3");
//WIFI METHOD START
mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
registerReceiver(new BroadcastReceiver()
{
@Override
public void onReceive(Context c, Intent intent)
{
results = mainWifi.getScanResults();
size = results.size();
ScanResult bestSignal = null;
for (ScanResult result : results) {
if (bestSignal == null
|| WifiManager.compareSignalLevel(bestSignal.level, result.level) < 0)
{
bestSignal = result;
}
}
msg = tics + " Strongest Network: " + bestSignal.SSID + " - Signal Strength:" + bestSignal.level;
new sendPostData().execute(Integer.toString(bestSignal.level) ,bestSignal.SSID.toString());
}
}, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
mainWifi.startScan();
//WIFI METHOD END
myFilterResponse.putExtra("serviceData", msg);
sendBroadcast(myFilterResponse);
Thread.sleep(1000);
} catch (Exception e){
e.printStackTrace();
}
}
}});
triggerService.start();
}
private class sendPostData extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
HttpPost request = new HttpPost(SERVICE_URI + "/StrongestWifi");
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
JSONStringer getWifiInfo;
try {
getWifiInfo = new JSONStringer()
.object()
.key("myWifiClass")
.object()
.key("SignalStrength").value(params[0])
.key("SSID").value(params[1])
.endObject()
.endObject();
StringEntity entity = new StringEntity(getWifiInfo.toString());
request.setEntity(entity);
// Send request to WCF service
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
Log.d("WebInvoke", "Saving : " + response.getStatusLine().getStatusCode());
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
}
}
这是我的日志猫:
>06-02 09:58:42.112: E/<<myServiceRunner-onStart>>(21871): I am alive-3!
>06-02 09:58:42.151: E/MAIN>>>(21871): message - receiving data 179736967
>06-02 09:58:42.252: E/SpannableStringBuilder(21871): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
>06-02 09:58:42.260: E/SpannableStringBuilder(21871): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
>06-02 09:58:42.276: E/SpannableStringBuilder(21871): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
>06-02 09:58:42.276: E/SpannableStringBuilder(21871): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
>06-02 09:58:43.127: W/dalvikvm(21871): threadid=18: thread exiting with uncaught exception (group=0x40d09930)
>06-02 09:58:43.135: E/MAIN>>>(21871): 2 Strongest Network: Chiameng - Signal Strength:-69 - receiving data 179737949
>06-02 09:58:43.166: E/AndroidRuntime(21871): FATAL EXCEPTION: AsyncTask #5
>06-02 09:58:43.166: E/AndroidRuntime(21871): java.lang.RuntimeException: An error occured while executing doInBackground()
>06-02 09:58:43.166: E/AndroidRuntime(21871): at android.os.AsyncTask$3.done(AsyncTask.java:299)
>06-02 09:58:43.166: E/AndroidRuntime(21871): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
>06-02 09:58:43.166: E/AndroidRuntime(21871): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
>06-02 09:58:43.166: E/AndroidRuntime(21871): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
>06-02 09:58:43.166: E/AndroidRuntime(21871): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
>06-02 09:58:43.166: E/AndroidRuntime(21871): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
>06-02 09:58:43.166: E/AndroidRuntime(21871): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
>06-02 09:58:43.166: E/AndroidRuntime(21871): at java.lang.Thread.run(Thread.java:856)
>06-02 09:58:43.166: E/AndroidRuntime(21871): Caused by: java.lang.IllegalArgumentException: Illegal character in scheme at index 0: 192.168.1.64:4567/AllocationService.svc/StrongestWifi
>06-02 09:58:43.166: E/AndroidRuntime(21871): at java.net.URI.create(URI.java:727)
>06-02 09:58:43.166: E/AndroidRuntime(21871): at org.apache.http.client.methods.HttpPost.<init>(HttpPost.java:79)
>06-02 09:58:43.166: E/AndroidRuntime(21871): at com.example.pcsprojectnetworkcodes.myServiceRunner$sendPostData.doInBackground(myServiceRunner.java:118)
>06-02 09:58:43.166: E/AndroidRuntime(21871): at com.example.pcsprojectnetworkcodes.myServiceRunner$sendPostData.doInBackground(myServiceRunner.java:1)
>06-02 09:58:43.166: E/AndroidRuntime(21871): at android.os.AsyncTask$2.call(AsyncTask.java:287)
>06-02 09:58:43.166: E/AndroidRuntime(21871): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
>06-02 09:58:43.166: E/AndroidRuntime(21871): ... 4 more
>06-02 09:58:44.166: E/MAIN>>>(21871): 2 Strongest Network: Chiameng - Signal Strength:-69 - receiving data 179738981
>06-02 09:58:44.409: W/dalvikvm(21871): threadid=12: thread exiting with uncaught exception (group=0x40d09930)
>06-02 09:58:44.409: I/Process(21871): Sending signal. PID: 21871 SIG: 9