Wednesday, 3 December 2014

How to keep  Asynctask and  Views in different classes ?
Before we start on that  we should take the fair Idea about Asynctask . 

What is asynctask ?

AsyncTask enables proper and easy use of the UI thread. This class allows 
to perform background operations and publish results on the UI thread without
having to manipulate threads and/or handlers.

An asynchronous task is defined by 3 generic types, called Params, 
Progress and Result, and 4 steps, called onPreExecute, doInBackground, 
onProgressUpdate and onPostExecute.

Views here Means Activity or fragments.

To make the User of Asyntask  clear and more structured. First make 
an Interface callback .
ICallback.java

package com.ikn.interfaces;

public interface ICallback<T> {

 public void getResponse(T result);
 
}  
Make an Asyntask class and call  the callback interface here 
to set the response.

DownloadWebPageTask.java
package com.ikn.task;

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

   ICallback<String> mICallback;

//Here contructor takes the Icallback in Constructor.

   public DownloadWebPageTask(ICallback<String> iCallback) {
        
        mICallback = iCallback;

    } 


    @Override

    protected String doInBackground(String... urls) {
      String response = "";
      for (String url : urls) {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        try {
          HttpResponse execute = client.execute(httpGet);
          InputStream content = execute.getEntity().getContent();

          BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
          String s = "";
          while ((s = buffer.readLine()) != null) {
            response += s;
          }

        } catch (Exception e) {
          e.printStackTrace();
        }
      }
      return response;
    }

    @Override
    protected void onPostExecute(String result) {
        mCallback.getResponse(result){
       }
   }
}
Now make An Activity where we call the asyntask.

public class MainActivity extends Activity {
 
 private TextView textView;
 private Button button;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    textView = (TextView) findViewById(R.id.TextView01);
    button = (Button) findViewById(R.id.button_01);
    buttonsetOnClickListener(getText());
  }


   private View.OnClickListener getText(){
        return new View.OnClickListener(){
            @Override
            public void onClick(View v) {
//Here we call the Asyntask  by passing Icallback in the contructor.
               DownloadWebPageTask task = new DownloadWebPageTask(getWebTextCallback());
              task.execute(new String[] { "http://www.socialcops.org" }); 
           }
        };
  }


//Here we do what  needs to be done  on Post method's result.
    private ICallback<String> getWebTextCallback( ){
        return new ICallback<String>() {
            @Override
            public void onSuccess(String result) {
               textView.setText(result) 
           }

       };
    }
}


No comments:

Post a Comment