Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to stop the AsyncTask if the result of fetching data is null?
    text
    copied!<p>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 :</p> <pre><code>class ProductConnect extends AsyncTask&lt;Boolean, String, String&gt; { 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&amp;user_name="+UserName+"&amp;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 &lt; 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&lt;String&gt;) lats); passValue.putStringArrayListExtra("veri", (ArrayList&lt;String&gt;) names); passValue.putStringArrayListExtra("longtitudes", (ArrayList&lt;String&gt;) 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(); } } </code></pre> <p>How should i follow a way ? Which methods should i use? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload