I want to cancel the asyncTask if my result string is null. ( I get a username and password from the user in a login activity and if this username and password don't exist in the database, the asyncTask has to cancel and ready to start the same task.) I read and applied something but they didn't run. Here is my AsyncTask :
class ProductConnect extends AsyncTask<Boolean, String, String> {
   public AsyncResponse delegate=null;
   private Activity activity;
   public void MyAsyncTask(Activity activity) {
        this.activity = activity;
    }
    @Override
    protected String doInBackground(Boolean... params) {
        String result = null;
        StringBuilder sb = new StringBuilder();
        try {
            // http post
            HttpClient httpclient = new DefaultHttpClient();
            HttpGet httppost = new HttpGet( "http://191.165.2.235/getProducts.php?login=1&user_name="+UserName+"&user_pass="+Password);
            HttpResponse response = httpclient.execute(httppost);
            if (response.getStatusLine().getStatusCode() != 200) {
                Log.d("MyApp", "Server encountered an error");
            }
          BufferedReader reader = new BufferedReader(
                    new InputStreamReader(
                            response.getEntity().getContent(), "UTF8"));  
            sb = new StringBuilder();
            sb.append(reader.readLine() + "\n");
            if(reader.readLine() == null){
                asyncTask.cancel(true);
            }
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
                if (isCancelled()) break;
            }
            result = sb.toString();
            Log.d("test", result);
        } 
        catch (Exception e) {
            Log.e("log_tag", "Error converting result " + e.toString());
        }
        return result;
    }
    @Override
    protected void onPostExecute(String result) {
        Intent passValue=new Intent(MainActivity.this, second.class);
        try {
            JSONArray jArray = new JSONArray(result);
            JSONObject json_data;
            for (int i = 0; i < jArray.length(); i++) {
                json_data = jArray.getJSONObject(i);
                t = json_data.getString("name");
                names.add(t);                                             
                latitude=json_data.getString("lat");
                lats.add(latitude);                                       
                longtitude=json_data.getString("lon");
                longts.add(longtitude);                                   
            }
            passValue.putStringArrayListExtra("latitudes", (ArrayList<String>) lats);
            passValue.putStringArrayListExtra("veri", (ArrayList<String>) names);
            passValue.putStringArrayListExtra("longtitudes", (ArrayList<String>) longts);
            startActivity(passValue);   
        } catch (JSONException e1) {
            e1.printStackTrace();
        } catch (ParseException e1) {
            e1.printStackTrace();
        }
        super.onPostExecute(result);
    }
     protected void onPreExecute()                          {
            super.onPreExecute();
            ProgressDialog pd = new ProgressDialog(MainActivity.this);
            pd.setTitle("Lütfen Bekleyiniz");
            pd.setMessage("Authenticating..");
            pd.show();
                                                                     }
}
How should i follow a way ? Which methods should i use?
 
     
     
    