我从AsyncTask中的数据库中提取数据,如果它为null,我想Toast一个警告文本。我在AsyncTask中尝试过,但我知道它不在工作线程中调用。这里是我的doInBackground方法:如何根据AsyncTask的结果在受保护的字符串doInBackground方法中调用Toast.makeText()?
protected String doInBackground(Boolean... params) {
String result = null;
StringBuilder sb = new StringBuilder();
try {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httppost = new HttpGet("http://191.162.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");
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
result = sb.toString();
if ( result == null ) {
// Toast.makeText(getApplicationContext(), "You entered wrong values.", Toast.LENGTH_LONG).show();
asyncTask.cancel(true);
Intent inte=new Intent(MainActivity.this, MainActivity.class);
inte.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
inte.addCategory(Intent.CATEGORY_HOME);
startActivity(inte);
}
}
catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
return result;
}
我取消了任务,创造在我的代码的新意图。你不关心他们。你看到Toast.makeText ..线,我尝试像这样当然会给出错误。我怎样才能做到这一点?如果它没有在AsyncTask中调用,我应该怎么调用?
你可以发布错误吗? –