返回类型与Asynctask不兼容

时间:2014-07-08 17:09:27

标签: android string android-asynctask

我正在尝试使用AsyncTask捕获httpclient的响应,但是在检查字符串中的任何不兼容性doInBackground时会抛出以下错误:

“返回类型与AsyncTask不兼容”

我尝试捕获httpclient的响应字符串并将其返回到doInBackground中,以便在onPostExecute中使用它。

有人可以帮助我。

谢谢

private String insertar(){  
            String response = "";
            HttpClient httpclient;
            List<NameValuePair> nameValuePairs;
            HttpPost httppost;
            httpclient=new DefaultHttpClient();
            httppost= new HttpPost("http://10.1.1.20/pruebaAndroid/insert.php"); // Url del Servidor      
            //Añadimos nuestros datos
            nameValuePairs = new ArrayList<NameValuePair>(4);
            nameValuePairs.add(new BasicNameValuePair("dni",dni.getText().toString().trim()));
            nameValuePairs.add(new BasicNameValuePair("nombre",nombre.getText().toString().trim()));
            nameValuePairs.add(new BasicNameValuePair("telefono",telefono.getText().toString().trim()));
            nameValuePairs.add(new BasicNameValuePair("email",email.getText().toString().trim()));

            try {
               httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));// envio las variables

               // capturo lo que devuelve, si no devolviera nada solo hago la ejecucion del httpclient sin el handler

               ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
               response = httpclient.execute(httppost, responseHandler);

               //----------------------------------------------------------------

            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return response;
        }

这是AsyncTask:

//AsyncTask para insertar Personas
    class Insertar extends AsyncTask<String,String,Boolean>{

        private Activity context;

        Insertar(Activity context){
            this.context=context;
        }
        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub


            String result = insertar();

            return result;
        }


        @Override
        protected void onPostExecute(Boolean result) {

        }
    }

1 个答案:

答案 0 :(得分:4)

这里的问题是AsyncTask Extensions是通用的,需要三种类型:AsyncTask<Params, Progress, Result> AsyncTask,它可以是Void或类,但不是原始数据类型。

所以会发生什么事情,你告诉编译器doInBackground返回一个原始var,但它期待一个类String的实例。

所以使用doInBackground函数作为

@Override
            protected String doInBackground(String... params) {
                // TODO Auto-generated method stub


                String result = insertar();

                return result;
            }

修改

在AsyncTask中,您返回Boolean并且在doInBackground中返回类型为String,所以

将其更改为String,因为您需要来自insertar()函数的字符串值,即。变化

class Insertar extends AsyncTask<String,String,Boolean>{

class Insertar extends AsyncTask<String,String,String>{

 @Override
        protected void onPostExecute(Boolean result) {

        }

 @Override
        protected void onPostExecute(String result) {

        }