在Textchanged上运行AsyncTask时出错

时间:2013-12-31 20:20:11

标签: android apache android-asynctask

我在片段中有一个EditText,用户将在其中输入用户名;当用户输入名称时,我想使用onTextChanged事件向服务器发出Http Post请求,并检查该用户名是否可用:它将返回一个状态代码,指示用户名是否可用。 当我运行它它给我一个错误。引起:java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序

private final TextWatcher mTextEditorWatcher = new TextWatcher() {

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        // When No Password Entered

    }

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        asycstuffs n = new asycstuffs();
        n.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, 0);
    }

    public void afterTextChanged(Editable s) {

    }
};

private class asycstuffs extends AsyncTask<Integer, HomeFeed_Obj, Integer> {

    @Override
    protected Integer doInBackground(Integer... params) {
        BufferedReader in = null;

        String s = Username.getText().toString();

        try {

            HttpClient httpclient = new DefaultHttpClient();
            HttpPut httput = new HttpPut("http://my url");
            String responseBody = "";
            HttpResponse response = null;
            try {

                httput.setEntity(new StringEntity(s, "UTF-8"));
                // Execute HTTP Post Request
                response = httpclient.execute(httput);
                // /

                HttpEntity entity = response.getEntity();
                Log.d("response ok", "username :/ " + response.getStatusLine().getStatusCode());

            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        // TODO Auto-generated method stub
        return null;
    }

}

1 个答案:

答案 0 :(得分:1)

你正试图在doInBackground()方法中访问ui元素!!

 String s= Username.getText().toString();

我认为你在这里遇到错误,尝试将字符串作为参数传递, 也发布你的logcat值