我从我的活动的onCreate方法调用异步任务,在异步中我正在解析子数组。我想用我得到的字符串更新我的活动的textView。 这是我的代码
public VerifyAnswerAsync(Context context, int userId){
this.context = context;
this.userId = userId;
}
@Override
protected String doInBackground(String... params) {
try {
url = new URL("URL");
urlConnection = url.openConnection();
inputStream = urlConnection.getInputStream();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
stringBuilder = new StringBuilder();
line = "";
while((line = bufferedReader.readLine())!=null){
stringBuilder.append(line);
}
inputStream.close();
result = stringBuilder.toString();
Log.d("TAG",result);
jsonArray = new JSONArray(result);
for(int i = 0; i<jsonArray.length();i++){
jsonObject = jsonArray.getJSONObject(i);
que1 = jsonObject.optString("que1");
ans1 = jsonObject.optString("ans1");
que2 = jsonObject.optString("que2");
ans2 = jsonObject.optString("ans2");
Log.d("Tag",que1);
Log.d("Tag",ans1);
Log.d("Tag",que2);
Log.d("Tag",ans2);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
this.publishProgress(que1,que2,ans1,ans2);
return result;
}
如何从异步任务更新活动中的文本视图。请帮助。
我尝试从postExecute执行此操作,但textView为nil。
答案 0 :(得分:0)
覆盖onPostExecute
对象上AsyncTask
来电setText(String text)
的{{1}}回调。