我正在尝试从异步任务发布一个吐司,并在堆栈上阅读这个答案:
Raising a Toast From AsyncTask
这些问题的快速摘要导致了这一点:
通过调用getApplicationContext()获取Context对象 MainActivity并将其作为参数传递给AsyncTask
我对如何通过异步任务传递上下文以及如何调用它感到困惑:
我的异步任务是:
public class ReadLogInJSON extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
return readJSONFeed(arg0[0]);
}
protected void onPostExecute(String result)
{
//decode json here
try{
JSONObject json = new JSONObject(result);
String status = json.getString("status");
if(status == "no"){
//toast logIN failed
String message = "Log In Failed";
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
else {
//get userName
//get user ID
//set preferences
//launch normal activity
}
}
catch(Exception e){
}
}
public String readJSONFeed(String URL) {
StringBuilder stringBuilder = new StringBuilder();
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(URL);
try {
HttpResponse response = httpClient.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
inputStream.close();
} else {
Log.d("JSON", "Failed to download file");
}
} catch (Exception e) {
Log.d("readJSONFeed", e.getLocalizedMessage());
}
return stringBuilder.toString();
}
}
我称之为:
new ReadLogInJSON().execute(url);
答案 0 :(得分:1)
您正在尝试在主线程上运行的onPostExecute中进行干预,因此您无需将上下文传递给您的任务。你应该能够做到这一点:
Toast.makeText(MyActivity.this, message, Toast.LENGTH_SHORT).show();
您也可以使用getApplicationContext()
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
答案 1 :(得分:0)
像这样称呼
ReadLogInJSON task = new ReadLogInJSON(this); // pass Context here to constructor
task .execute(url);
并在AsyncTask中创建一个构造函数,如下所示
public class ReadLogInJSON extends AsyncTas <String, Void, String> {
Context c;
public ReadLogInJSON(Context context)
{
c = context; // assign Context in constructor here
}
@Override1
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
return readJSONFeed(arg0[0]);
然后将Toast
中的onPostExecute()
更改为此
Toast.makeText(c, message, Toast.LENGTH_SHORT).show();
Toast
需要Context
,因此您将Activity
Context
通过构造函数并使用它来显示Toast
从来没有任何理由或想要从getApplicationContext()
为Activity
致电Toast
。 Toast
需要Activity Context
Activity
显然已经拥有this
指的是
答案 2 :(得分:0)
你应该尝试的一件事,IMO,无论如何都是很好的编程习惯,如果你需要一个Context对象,那么你应该在AsyncTask子类的构造函数中声明它,而不是使用从父类继承的空构造函数。如果你的doInBackground()函数成功执行,那么你应该下载到onPostExecute()。尝试在doInBackground()的末尾和onPostExecute()的开头记录一些内容,以便您知道已完成一个回调并转换到流程中的下一个回调。
在这种情况下,您的代码看起来更像是:
public class ReadLogInJSON extends AsyncTask<String, Void, String> {
private Context mContext;
public ReadLoginJSON(Context context) {
this.mContext = context;
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
return readJSONFeed(arg0[0]);
}
@Override
public void onPostExecute(String result){
// DO YOUR THING HERE
Toast.makeText(this.mContext, "I Executed!", Toast.LENGTH_SHORT).show();
}