-4
String json_string;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void getJSON (View view){
    new BackgroundTask().execute();
}

class BackgroundTask extends AsyncTask <Void, Void, String>{
    String json_url;
    @Override
    protected void onPreExecute() {
        json_url = "http://androidtut.comli.com/json_get_data.php";
    }

    @Override
    protected Void doInBackground(Void... voids) {
        try {
            URL url = new URL(json_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder stringBuilder = new StringBuilder();

            while ((json_string = bufferedReader.readLine()) != null) {

                stringBuilder.append(json_string + "\n");
            }
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return stringBuilder.toString().trim();


        }
        catch (MalformedURLException e){
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String result) {
        TextView textView = (TextView) findViewById(R.id.textJSON);
        textView.setText(result);
    }
}

Error:(31, 5) error: MainActivity.BackgroundTask is not abstract and does not override abstract method doInBackground(Void...) in AsyncTask

Error:(39, 24) error: doInBackground(Void...) in MainActivity.BackgroundTask cannot override doInBackground(Params...) in AsyncTask return type Void is not compatible with String where Params,Result are type-variables: Params extends Object declared in class AsyncTask Result extends Object declared in class AsyncTask

Error:(38, 9) error: method does not override or implement a method from a supertype

Error:(54, 53) error: incompatible types: String cannot be converted to Void

Error:Execution failed for task ':app:compileDebugJavaWithJavac'.> Compilation failed; see the compiler error output for details.

5 Answers5

2

AsyncTask <Void, Void, String>

The last type here is the return result type.

Your doInBackground is currently defined with Void, not String

Likewise, you cannot return a StringBuilder String result as a Void type.


You may find that the other Android HTTP libraries that deal with JSON are much easier to use.

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

AFAIK, when you define Void in AsyncTask , you should understand the meaning.

  1. doInBackground is a function with empty argument (the first void), return a String (the last String)

    protected String doInBackground()

  2. onProgressUpdate is a function with empty argument (the 2nd void)

    protected void onProgressUpdate()

  3. onPostExecute is a function with String argument (value from step 1).

    protected void onPostExecute(String)

kidnan1991
  • 366
  • 2
  • 12
0

If you can't understand the function in AsyncTask clearly,you'd better try to generate the function in AndroidStudio. Here is the code generated by AndroidStudio.

class BackgroundTask extends AsyncTask<Void,Void,String>
{
    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected String doInBackground(Void... params) {
        return null;
    }
}

Compare your code with above,I think you would kown the answer.

orzangleli
  • 178
  • 7
0

I found two mistakes here.
1. For task execution,

new BackgroundTask().execute(null);


2. doInBackground return type should match with Result type (i.e. String)

protected String doInBackground(Void... voids)

AsyncTask

    String json_string;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void getJSON (View view){
        new BackgroundTask().execute(null);
    }

    class BackgroundTask extends AsyncTask <Void, Void, String>{
        String json_url;
        @Override
        protected void onPreExecute() {
            json_url = "http://androidtut.comli.com/json_get_data.php";
        }

        @Override
        protected String doInBackground(Void... voids) {
            try {
                URL url = new URL(json_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuilder stringBuilder = new StringBuilder();

                while ((json_string = bufferedReader.readLine()) != null) {

                    stringBuilder.append(json_string + "\n");
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return stringBuilder.toString().trim();


            }
            catch (MalformedURLException e){
                e.printStackTrace();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(String result) {
            TextView textView = (TextView) findViewById(R.id.textJSON);
            textView.setText(result);
        }
    }
pathe.kiran
  • 2,444
  • 1
  • 21
  • 27
-1
String json_string;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void getJSON (View view){
    new AsynsTask(){
        String json_url;
        @Override
        protected void onPreExecute() {
            json_url = "http://androidtut.comli.com/json_get_data.php";
        }

        @Override
        protected Void doInBackground(Void... voids) {
            try {
                URL url = new URL(json_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuilder stringBuilder = new StringBuilder();

                while ((json_string = bufferedReader.readLine()) != null) {

                    stringBuilder.append(json_string + "\n");
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return stringBuilder.toString().trim();


            }
            catch (MalformedURLException e){
                e.printStackTrace();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(String result) {
            TextView textView = (TextView) findViewById(R.id.textJSON);
            textView.setText(result);
        }
    }.execute();

 }
imdungnguyen
  • 216
  • 1
  • 3
  • 14