gpt4 book ai didi

java - 关于任务 onPostExecute

转载 作者:行者123 更新时间:2023-11-29 04:50:26 25 4
gpt4 key购买 nike

我有一个运行良好的 AsyncTask 类,但在这个类中,我在 onPreExecute 中有一个 ProgressDialog,它工作正常,但我需要在完成进程对话框关闭时,我将代码放在 onPostExecute 中,这方式:

protected void onPostExecute() {
progress.dismiss();
}

但它什么也没做。该对话框不会关闭。这是所有代码:

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();
}

所以我调用:

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);
}
}

最佳答案

你没有调用正确的 onPostExecute:

你需要:

@Override
protected void onPostExecute(ArrayList<String> data) {
super.onPostExecute(data);
progress.dismiss();
}

问题是您没有通过添加 onPostExecute() 来覆盖 AsyncTask 类中的方法。您需要添加 @Override 注释并传入 AsyncTask 类的返回类型。

关于java - 关于任务 onPostExecute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35637463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com