Search code examples
androidandroid-asynctaskandroid-internet

AsyncTask Android on main thread


I'm trying to implement an asynchronous access to internet using AsyncTask, but in log cat PID and TID of my logging are the same, because AsyncTask doesn't create a parallel queue, so my app crashes with a NetworkOnMainThreadException.

Here's my subclass code :

class BL_SimpleAsyncTask extends AsyncTask<Void, Void, Void> {

        String requestServer;
        HashMap<String, String> postRequestBody;
        //------------------------// answer from http client
        static DefaultHttpClient sharedClient = null;

        boolean isPostRequest;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... params) {
            System.out.println("bg started");

            if (sharedClient == null) {
                sharedClient = new DefaultHttpClient();
            }

            HttpPost post = new HttpPost(requestServer);
            String postBody = new String();
            postBody += "{";
            for (String key : postRequestBody.keySet()) {
                String result = String.format("\"%s\":\"%s\",", key, postRequestBody.get(key));
                postBody += result;

            }

            System.out.println("body initialized");


            postBody.substring(0, postBody.length() - 1);
            postBody += "}";
            try {
                post.setEntity(new StringEntity(postBody));
            } catch (UnsupportedEncodingException e) {
                System.out.println(e.getMessage());
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            System.out.println("entity set");


            try {
                if (post != null) {
                    System.out.println("starting request....");
                    HttpResponse response = sharedClient.execute(post);
                    System.out.println("responce recieved");
                } else {
                    System.out.println("null request");

                }
                // System.out.println(response) ;

            } catch (ClientProtocolException e) {
                System.out.println(e.getMessage());

                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                System.out.println(e.getMessage());

                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
        }
    }

}

So, to start post-request, I simply do the following :

    BL_SimpleAsyncTask obj = new BL_SimpleAsyncTask() ;
    obj.requestServer = "https://api.orbios.com/v1/auth/sign-in" ;
    obj.postRequestBody = new HashMap<String, String> () ;
    obj.postRequestBody.put ("password", password) ;
    obj.postRequestBody.put("email", email ) ;
    obj.isPostRequest = true ;
    System.out.println("start bg thread") ;

    obj.doInBackground() ;

What am I doing wrong?


Solution

  • You are not supposed to call doInBackground() yourself. Just call execute() and let the framework call your doInBackground() in a background thread.