title: adding AsyncTask to onClick listener
body: <p>I have made program which works with AsyncTask it prints a list of the JSON data when program is executed, but the problem is that I want it to execute when I press the button. How do I get the results of AsyncTask into my onClick Button Listener ? How do I call AsyncTask from onClick? </p>
  
  <pre><code>public class MainActivity extends ActionBarActivity implements LocationListener {
  
  
      double[] avstand;
      JSONArray jArray;
      LocationManager lm;
      TextView lt, ln;
      String provider;
      Location l;
      ListView list;
      StasjonAdapter adapter;
      ArrayList&lt;Stasjoner&gt; stasjonListe;
      Button nButton;
      TextView test;
      GPSTracker gps;
      Stasjoner stasjon;
      double minValue;
      String geoUri;
      Button instButton;
      Button bildeButton;
      Button kjorTil;
      Button visListe;
      File mPhotoFile;
      Uri mPhotoFileUri;
      String mDisplayFolder;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          setContentView(R.layout.activity_main);
          ln=(TextView)findViewById(R.id.visLong);
          test=(TextView)findViewById(R.id.test);
          lt=(TextView)findViewById(R.id.visLati);
  
          gps = new GPSTracker(MainActivity.this);
  
          // check if GPS enabled
          if(gps.canGetLocation()){
  
              double latitude = gps.getLatitude();
              double longitude = gps.getLongitude();
  
              // \n is for new line
             // Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
          }else{
              // can't get location
              // GPS or Network is not enabled
              // Ask user to enable GPS/network in settings
              gps.showSettingsAlert();
          }
     //get latitude and longitude of the location
              double lat= gps.getLatitude();
              double lng= gps.getLongitude();
              //display on text view
              ln.setText(""+lng);
              lt.setText(""+lat);
  
          list = (ListView)findViewById(R.id.list);
          stasjonListe = new ArrayList&lt;Stasjoner&gt;();
          nButton = (Button)findViewById(R.id.button1);
          instButton = (Button)findViewById(R.id.instillinger);
          bildeButton = (Button)findViewById(R.id.bildeButton);
          kjorTil= (Button)findViewById(R.id.button);
          visListe = (Button)findViewById(R.id.visListe);
          //new StasjonAsyncTask().execute("http://hotell.difi.no/?dataset=porsgrunn/elbilparkering?");       blogas url, jis grazina html puslapi, reikia daryt requesta i api endpoint, kuris grazina json
          new StasjonAsyncTask().execute("http://hotell.difi.no/api/json/porsgrunn/elbilparkering?");
  
          instButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Intent intent = new Intent(MainActivity.this, Instillinger.class);
                  startActivity(intent);
              }
          });
          nButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  int value = (int) getMinValue(avstand);
                  test.setText(stasjonListe.get(value).getLatitude().toString());
                  test.setText(stasjonListe.get(value).getLongtitude().toString());
                  String geoURI = "geo:" + stasjonListe.get(value).getLatitude().toString() + ","+ stasjonListe.get(value).getLongtitude().toString() + "?z=";
                  Uri geo = Uri.parse(geoURI);
                  Intent geoMap = new Intent(Intent.ACTION_VIEW,geo);
                  startActivity(geoMap);
              }
          });
  
          visListe.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  //StasjonAsyncTask
  
              }
          });
  
          bildeButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Intent intent = new Intent(MainActivity.this, CustomCameraActivity.class);
                  startActivity(intent);
              }
          });
          kjorTil.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
                          Uri.parse("http://maps.google.com/maps?saddr=" + gps.getLatitude() +"," + gps.getLongitude() + "&amp;daddr=" + Double.parseDouble(stasjon.getLatitude()) +","+Double.parseDouble(stasjon.getLongtitude())));
                  startActivity(intent);
              }
          });
  
      }
  
      @Override
      public void onLocationChanged(Location location) {
  
      }
  
      @Override
      public void onStatusChanged(String provider, int status, Bundle extras) {
  
      }
  
      @Override
      public void onProviderEnabled(String provider) {
  
      }
  
      @Override
      public void onProviderDisabled(String provider) {
  
      }
  
  
      public class StasjonAsyncTask extends AsyncTask&lt;String, Void, Boolean&gt;{
  
          @Override
          protected Boolean doInBackground(String... urls) {
              try {
              HttpClient client = new DefaultHttpClient();
              //HttpPost post = new HttpPost(urls[0]);    api endpointas nepriima post requestu, pakeiciam i get
              HttpGet get = new HttpGet(urls[0]);
              HttpResponse response = client.execute(get);
  
              int status = response.getStatusLine().getStatusCode();
                  if(status == 200) {
                       HttpEntity entity = response.getEntity();
                      String data = EntityUtils.toString(entity);
  
                      JSONObject jObj = new JSONObject(data);
                      jArray = jObj.getJSONArray("entries");
                      avstand = new double[jArray.length()];
  
                      for(int i=0;i&lt; jArray.length();i++){
                          stasjon = new Stasjoner();
  
                          JSONObject jRealObject = jArray.getJSONObject(i);
  
  
                          stasjon.setId(jRealObject.getString("id"));
                          stasjon.setKapasitet(jRealObject.getString("kapasitet"));
                          stasjon.setPlassering(jRealObject.getString("plassering"));
                          stasjon.setTilgjengelighet(jRealObject.getString("tilgjengelighet"));
                          stasjon.setBeskrivelse(jRealObject.getString("beskrivelse"));
                          stasjon.setNavn(jRealObject.getString("navn"));
                          stasjon.setLongtitude(jRealObject.getString("longitude"));
                          stasjon.setLatitude(jRealObject.getString("latitude"));
                          stasjon.setTilgang(jRealObject.getString("tilgang"));
                          stasjon.setTidsbegrensing(jRealObject.getString("tidsbegrensing"));
  
                          double dist = distance(Double.parseDouble(stasjon.getLatitude()), Double.parseDouble(stasjon.getLongtitude()), gps.getLatitude(),gps.getLongitude(), 'K');
                          stasjon.setAvstand(Double.toString(dist));
  
                         avstand[i]= dist;
  
                          stasjonListe.add(stasjon);
                      }
                      return true;
  
  
                  }
  
  
  
  
              } catch (IOException e) {
                  e.printStackTrace();
              } catch (JSONException e) {
                  e.printStackTrace();
              }
              return false;
          }
          protected void onPostExecute(Boolean result) {
              super.onPostExecute(result);
              //Arrays.sort(avstand);
  
              minValue = getMinValue(avstand);
  
              //test.setText(Double.toString(minValue));
              if(result== false){
  
              Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
  
              } else {
                  StasjonAdapter adapter = new StasjonAdapter(getApplicationContext(),R.layout.rad, stasjonListe);
                  list.setAdapter(adapter);
              }
          }
  
          private double distance(double lat1, double lon1, double lat2, double lon2, char unit) {
              double theta = lon1 - lon2;
              double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
              dist = Math.acos(dist);
              dist = rad2deg(dist);
              dist = dist * 60 * 1.1515;
              if (unit == 'K') {
                  dist = dist * 1.609344;
              } else if (unit == 'N') {
                  dist = dist * 0.8684;
              }
              return (dist);
          }
          private double deg2rad(double deg) {
              return (deg * Math.PI / 180.0);
          }
          private double rad2deg(double rad) {
              return (rad * 180.0 / Math.PI);
          }
      }
  
      public static double getMinValue(double[] numbers){
          double minValue = numbers[0];
          int minValueIndex = 0;
          for(int i=1;i&lt;numbers.length;i++){
              if(numbers[i] &lt; minValue){
                  minValue = numbers[i];
                  minValueIndex = i;
              }
          }
          return minValueIndex;
      }
  
  }
  </code></pre>