关于任务onPostExecute [英] About task onPostExecute
问题描述
我有一个类的AsyncTask运行良好,但在这个班我在上preExecute,该工作确定有一个ProgressDialog,但我需要当我完成的过程中processdialog接近,我把code在onPostExecute,这种方式:
保护无效onPostExecute(){
progress.dismiss();
}
不过,这并不做任何事情。该对话框不会解雇。
这是所有code:
公共类JsonRequest扩展的AsyncTask<弦乐,无效的ArrayList<串GT;> {私人最终上下文的背景下;
私人的ArrayList<串GT;字符串数组=新的ArrayList<串GT;();
私人ProgressDialog进展情况;公共JsonRequest(上下文C){
this.context = C;
}在preExecute保护无效(){
进度=新ProgressDialog(this.context);
progress.setMessage(加载);
progress.show();
}@覆盖
保护的ArrayList<串GT; doInBackground(字符串... PARAMS){
尝试{ 最后弦乐POST_PARAMS =参数[1]; URL = OBJ新的URL(PARAMS [0]);
HttpURLConnection的CON =(HttpURLConnection类)obj.openConnection();
con.setRequestMethod(POST);
con.setRequestProperty(用户代理,Mozilla的/ 5.0); //仅适用于POST - START
con.setDoOutput(真);
OutputStream的OS = con.getOutputStream();
os.write(POST_PARAMS.getBytes());
os.flush();
os.close();
//仅适用于POST - END INT响应code = con.getResponse code();
的System.out.println(POST响应code ::+响应code); 如果(响应code == HttpURLConnection.HTTP_OK){//成功
在的BufferedReader =新的BufferedReader(新的InputStreamReader(
con.getInputStream()));
串inputLine;
StringBuffer的响应=新的StringBuffer(); 而((inputLine = in.readLine())!= NULL){
response.append(inputLine);
}
附寄(); JSONArray myListsAll =新JSONArray(response.toString());
的for(int i = 0; I< myListsAll.length();我++){ 的JSONObject的JSONObject = myListsAll.getJSONObject(I)
this.stringArray.add(jsonObject.toString());
} }其他{
的System.out.println(POST请求没有工作);
}
}赶上(MalformedURLException的E){
// TODO自动生成catch块
e.printStackTrace();
}赶上(的ProtocolException E){
e.printStackTrace();
}赶上(IOException异常五){
e.printStackTrace();
}赶上(JSONException E){
e.printStackTrace();
}
返回this.stringArray;
}保护无效onPostExecute(){
progress.dismiss();
}
所以,我呼吁:
公共类MyActivity扩展AppCompatActivity实现AdapterView.OnItemClickListener {
私人无效createListView(){
itens =新的ArrayList< ItemFeedsListView>(); 的String [] = itensUrl {URL,数据};
JsonRequest任务=新JsonRequest(本);
尝试{
ArrayList的<串GT;结果= task.execute(itensUrl)获得(); 的for(int i = 0; I< result.size();我++){
串currentList = result.get(ⅰ);
的JSONObject的JSONObject =新的JSONObject(currentList); ItemFeedsListView项目=新ItemFeedsListView(jsonobject.optString(\"value1\"),jsonobject.optString(\"value2\"),jsonobject.optString(\"valeu3\"),jsonobject.optString(\"value4\"),jsonobject.optString(\"value5\"), R.mipmap.eu,R.mipmap.logo);
itens.add(项目); } }赶上(InterruptedException的E){
e.printStackTrace();
}赶上(为ExecutionException E){
e.printStackTrace();
}赶上(JSONException E){
e.printStackTrace();
} // CRIA O适配器
adapterListView =新AdapterFeedsListView(这一点,itens); //定义O适配器
listView.setAdapter(adapterListView);
//心病quando一个LISTAéselecionada第ralagem。
listView.setCacheColorHint(Color.TRANSPARENT);
}
}
您不能调用正确的onPostExecute:
您需要:
@覆盖
保护无效onPostExecute(ArrayList的<串GT;数据){
super.onPostExecute(数据);
progress.dismiss();
}
问题是你不只是增加一个onPostExecute()覆盖在AsyncTask的类中的方法。你需要添加@Override注释,并通过在你的AsyncTask类的返回类型。
祝你好运和快乐编码!
I have a class AsyncTask that worked well, but in this class I have a ProgressDialog in the onPreExecute, that worked ok, but I need that when I finish the process the processdialog close, I put the code in the onPostExecute, this way:
protected void onPostExecute() {
progress.dismiss();
}
But it doesn't do anything. The dialog doesn't dismiss. This is the all code:
public class JsonRequest extends AsyncTask<String, Void, ArrayList<String>> {
private final Context context;
private ArrayList<String> stringArray = new ArrayList<String>();
private ProgressDialog progress;
public JsonRequest(Context c){
this.context = c;
}
protected void onPreExecute(){
progress= new ProgressDialog(this.context);
progress.setMessage("Loading");
progress.show();
}
@Override
protected ArrayList<String> doInBackground(String... params) {
try {
final String POST_PARAMS = params[1];
URL obj = new URL(params[0]);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
// For POST only - START
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
os.write(POST_PARAMS.getBytes());
os.flush();
os.close();
// For POST only - END
int responseCode = con.getResponseCode();
System.out.println("POST Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) { //success
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
JSONArray myListsAll= new JSONArray(response.toString());
for(int i=0;i<myListsAll.length();i++){
JSONObject jsonObject = myListsAll.getJSONObject(i);
this.stringArray.add(jsonObject.toString());
}
} else {
System.out.println("POST request not worked");
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return this.stringArray;
}
protected void onPostExecute() {
progress.dismiss();
}
So I call:
public class MyActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
private void createListView() {
itens = new ArrayList<ItemFeedsListView>();
String[] itensUrl = {"url","data"};
JsonRequest task = new JsonRequest(this);
try {
ArrayList<String> result = task.execute(itensUrl).get();
for(int i = 0 ; i < result.size() ; i++) {
String currentList = result.get(i);
JSONObject jsonobject = new JSONObject(currentList);
ItemFeedsListView item = new ItemFeedsListView(jsonobject.optString("value1"),jsonobject.optString("value2"),jsonobject.optString("valeu3"),jsonobject.optString("value4"),jsonobject.optString("value5"), R.mipmap.eu, R.mipmap.logo);
itens.add(item);
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
//Cria o adapter
adapterListView = new AdapterFeedsListView(this, itens);
//Define o Adapter
listView.setAdapter(adapterListView);
//Cor quando a lista é selecionada para ralagem.
listView.setCacheColorHint(Color.TRANSPARENT);
}
}
your not calling the right onPostExecute:
you need:
@Override
protected void onPostExecute(ArrayList<String> data) {
super.onPostExecute(data);
progress.dismiss();
}
The problem is your not overriding the method in AsyncTask class just by adding an onPostExecute(). you need to Add @Override annotation and pass in the return type of your AsyncTask Class.
Good luck and Happy Coding!
这篇关于关于任务onPostExecute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!