Search code examples
javaandroidapiandroid-asynctaskexecute

Asynctask unknown type execute


This is my first time with getting APIS to return the result JSON object. I think I have got the async task code right but I just don't know how to execute it. This is my class code. For my layout all I have is one button with an onClick () method gg, a progress bar and one text view.
This is the async task:

public class MainActivity extends Activity 
{
    ProgressBar progressBar;
    TextView responseView;
  EditText emailText;
    String URL;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
            progressBar = (ProgressBar) findViewById(R.id.progressBar);
            responseView = (TextView) findViewById(R.id.responseView);
            emailText = (EditText) findViewById(R.id.emailText);
            URL = "https://kgsearch.googleapis.com/v1/entities:search?query=taylor+swift&key=APIKEY&limit=1&indent=True";
}
    public void gg(View v)
    {
            new RetrieveFeedTask.execute();
    }


    private class RetrieveFeedTask extends AsyncTask<Void, Void, String> {

            private Exception exception;

            protected void onPreExecute() {
                    progressBar.setVisibility(View.VISIBLE);
                    responseView.setText("");
                    Toast.makeText(MainActivity.this, "pre execute", Toast.LENGTH_LONG).show();
            }

            protected String doInBackground(Void... urls) {
                    String email = emailText.getText().toString();
                    // Do some validation here


                    try {
                            URL url = new URL(URL);
                            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                            try {
                                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                                    StringBuilder stringBuilder = new StringBuilder();
                                    String line;
                                    while ((line = bufferedReader.readLine()) != null) {
                                            stringBuilder.append(line).append("\n");
                                    }
                                    bufferedReader.close();
                                    return stringBuilder.toString();
                            }
                            finally{
                                    urlConnection.disconnect();
                            }
                    }
                    catch(Exception e) {
                            Log.e("ERROR", e.getMessage(), e);
                            return null;
                    }
            }

            protected void onPostExecute(String response) {
                    if(response == null) {
                            response = "THERE WAS AN ERROR";
                            Toast.makeText(MainActivity.this, "post execute", Toast.LENGTH_LONG).show();
                    }
                    progressBar.setVisibility(View.GONE);
                    Log.i("INFO", response);
                    responseView.setText(response);
            }
    }


    }

So in the public void gg(View v) I call the .execute method but it gives me an error

Unknown type execute

Do I have to add some params to the execute method?
If so what?

Thanks.


Solution

  • Try

    new RetrieveFeedTask().execute();