简体   繁体   中英

AsyncTask crashing Android Service

my AsyncCode and Service code is running fine, but when i merge them together it crashes my application's GUI, the code manages to write to the database. I am new to android development so i do not quite know why it crashes.

This is my code for the method

@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) {
    }
}

And here is my log cat:

>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

There seems to be a problem here:

Illegal character in scheme at index 0

which refers to this line here:

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

Most likely you have a space at the beginning of the SERVICE_URI ?

I would bet that you have a space (' ') in front of the URL that you're passing:

192.168.1.64:4567/AllocationService.svc/StrongestWifi

Double-check that. You might just need to do a String#trim() on the URL.

There is a probleme with your SERVICE_URI variable. Maybe a misplaced space. Or maybe you have forgotten the "http://" (or equivalent) at the beginning...

In Your Async Task you should have Return type of doInBackground and postExecute must be same . And You are return null that why it give you error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM