AsyncTask崩溃Android服务

时间:2013-06-02 10:02:02

标签: java android android-asynctask

我的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

4 个答案:

答案 0 :(得分:1)

这里似乎有问题:

Illegal character in scheme at index 0

这里引用这一行:

HttpPost request = new HttpPost(SERVICE_URI + "/StrongestWifi");

您很可能在SERVICE_URI

的开头有空格

答案 1 :(得分:1)

我敢打赌,你传递的网址前面有一个空格(''):

192.168.1.64:4567/AllocationService.svc/StrongestWifi

仔细检查。您可能只需要在网址上执行String#trim()

答案 2 :(得分:1)

您的SERVICE_URI变量存在问题。可能是一个错位的空间。 或者你可能在开始时忘记了“http://”(或等效的)......

答案 3 :(得分:0)

在您的异步任务中,您应该具有doInBackground的返回类型,并且postExecute必须相同。并且你返回null,为什么它会给你错误