我有一個類AsyncTask,運行良好,但在這個類中,我有onPreExecute ProgressDialog,工作正常,但我需要,當我完成該進程processdialog關閉,我把代碼放在onPostExecute,這樣:關於任務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);
}
}
你好道格,你是對的,用這種方式uithread被封,這個話題是如何創建的修復有關進程加載錯誤的inner_class7顯示來解決,我改變了我的代碼像你建議並創建其他話題來談論。我希望你把你的意見放在那裏。感謝感謝這是鏈接:http://stackoverflow.com/questions/35685688/suggestion-about-asynctask –