1

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

4 回答 4

1

这里似乎有问题:

Illegal character in scheme at index 0

这里指的是这一行:

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

最有可能你在开头有一个空格SERVICE_URI

于 2013-06-02T10:08:30.580 回答
1

我敢打赌,您要传递的 URL 前面有一个空格 (' '):

192.168.1.64:4567/AllocationService.svc/StrongestWifi

仔细检查。您可能只需要对String#trim()URL 进行操作。

于 2013-06-02T10:08:38.280 回答
1

你的SERVICE_URI变量有问题。也许是一个错位的空间。或者,您可能忘记了开头的“http://”(或等效项)...

于 2013-06-02T10:12:04.797 回答
0

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

于 2013-06-02T10:09:41.350 回答