即使有AsyncTask,也会发生NetworkOnMainThreadException

时间:2015-07-20 04:26:18

标签: android-asynctask

我是Android应用开发的新手。我已阅读了几篇帖子,建议我必须使用AsyncTask进入PHP文件并编辑数据库。

以下是我的AsyncTask课程。

private class urlEditTask extends AsyncTask<Void, Void, Void>{

    final String propName;
    final String roomLoc;
    final String roomType;

    public urlEditTask(final String propName, final String roomLoc, final String roomType){
        super();
        this.propName = propName;
        this.roomLoc = roomLoc;
        this.roomType = roomType;
    }


    @Override
    protected Void doInBackground(Void... params) {
        editDB(propName, roomLoc, roomType);


        return null;
    }

    protected void onPostExecute(Void... params) {
        Toast.makeText(getApplicationContext(), "Loading....", Toast.LENGTH_LONG).show();
    }

    private void editDB(final String propName, final String roomLoc, final String roomType){

        String name = etName.getText().toString();
        String contact = etContact.getText().toString();
        String email = etEmail.getText().toString();
        String rentStart = etRentStart.getText().toString();
        String rentEnd = etRentEnd.getText().toString();

        List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(1);
        nameValuePair.add(new BasicNameValuePair("name", name));
        nameValuePair.add(new BasicNameValuePair("propName", propName));
        nameValuePair.add(new BasicNameValuePair("roomLoc", roomLoc));
        nameValuePair.add(new BasicNameValuePair("roomType", roomType));
        nameValuePair.add(new BasicNameValuePair("contact", contact));
        nameValuePair.add(new BasicNameValuePair("email", email));
        nameValuePair.add(new BasicNameValuePair("rentStart", rentStart));
        nameValuePair.add(new BasicNameValuePair("rentEnd", rentEnd));

        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://hoome.hk/EditDB.php");
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
            HttpResponse response = httpClient.execute(httpPost);

            HttpEntity entity = response.getEntity();

            InputStream is = entity.getContent();

            String msg = "Data entered successfully";
            Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            Log.e("ClientProtocol", "Log_tag");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

我希望在单击按钮后执行任务。 这是我的执行代码。

Button confirmEdit = (Button) findViewByID(R.id.confirmEdit);
confirmEdit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        new urlEditTask(itemValue1, itemValue2, itemValue3).execute();

    }
 });

可能,我不太了解AsyncTask的工作原理。 我可以知道为什么我会使用上述代码获得NetworkOnMainThreadException吗?

这是日志:

    07-20 04:14:01.480    2588-2603/hk.hoome.www.contracthandler E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: hk.hoome.www.contracthandler, PID: 2588
java.lang.RuntimeException: An error occured while executing doInBackground()

        at android.os.AsyncTask$3.done(AsyncTask.java:300)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:818)
 Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

        at android.os.Handler.<init>(Handler.java:200)
        at android.os.Handler.<init>(Handler.java:114)
        at android.widget.Toast$TN.<init>(Toast.java:336)
        at android.widget.Toast.<init>(Toast.java:100)
        at android.widget.Toast.makeText(Toast.java:250)

        at hk.hoome.www.contracthandler.MainActivity$urlEditTask.doInBackground(MainActivity.java:443)

        at hk.hoome.www.contracthandler.MainActivity$urlEditTask.doInBackground(MainActivity.java:399)
        at android.os.AsyncTask$2.call(AsyncTask.java:288)

        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)    

        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)

        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)                                                                                     
        at java.lang.Thread.run(Thread.java:818)

0 个答案:

没有答案