主线程上的AsyncTask Android

时间:2014-08-29 13:36:13

标签: android android-asynctask android-internet

我正在尝试使用AsyncTask实现对互联网的异步访问,但在我的日志记录的日志cat PIDTID中是相同的,因为AsyncTask没有&#39 ; t创建一个并行队列,因此我的应用程序崩溃了NetworkOnMainThreadException

这是我的子类代码:

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);
        }
    }

}

因此,要开始发布请求,我只需执行以下操作:

    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() ;

我做错了什么?

2 个答案:

答案 0 :(得分:3)

您应该调用doInBackground()方法,而不是直接调用execute

答案 1 :(得分:3)

您不应该自己致电doInBackground()。只需致电execute(),让框架在后台主题中调用doInBackground()