Home » Java » AsyncTask … class must be declared abstract or implement abstract method 'doInBackground(Params…)' in 'AsyncTask' still giving error

AsyncTask … class must be declared abstract or implement abstract method 'doInBackground(Params…)' in 'AsyncTask' still giving error

Posted by: admin July 23, 2018 Leave a comment

Questions:

How do I get the doInBackground() method to be implemented as an abstract method ? The IDE also says I require 3 arguments instead of just 1 for AsyncTask<…>

package com.example.a_phi.nowswap;

import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

public class EstablishConnection extends AsyncTask<String>{

    StringBuilder sb = new StringBuilder();

    public String doInBackground(String id) {

        String link = "http://213.251.43.215/addContact.php";

        try {
            URL url = new URL(link);
            String data = URLEncoder.encode("id", "UTF-8")
                    + "=" + URLEncoder.encode(id, "UTF-8");

            URLConnection conn = url.openConnection();
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(data);
            wr.flush();
            BufferedReader reader = new BufferedReader(new
                    InputStreamReader(conn.getInputStream()));

            String line;

            // Read Server Response
            while((line = reader.readLine()) != null) {
                sb.append(line);
                break;
            }

        }catch(MalformedURLException e) {
            System.out.print("MalformedURLException" + e.getMessage());
            e.printStackTrace();
        }
        catch(IOException e){
            System.out.print("IOException"+e.getMessage());
            e.printStackTrace();
        }

        return sb.toString();
}
}

How do I get the doInBackground() method to be implemented as an abstract method ? The IDE also says I require 3 arguments instead of just 1 for AsyncTask<…>

Answers: