我有一個名爲RestClient
的類,它從我的webService獲取一些信息,然後返回,我試圖讓一個Progress對話框在訪問Internet時運行。當我在不止一個地方使用這個課程時,我不會在活動本身中進行。這裏是我RestClient
類:ProgressDialog在另一個AsyncTask類中不顯示什麼時候從Activity調用
public class RestClient extends AsyncTask<URL, String, String> {
private Context context;
private String string;
public RestClient(Context context, String string)
{
this.context = context;
this.string = string;
}
@Override
protected void onPreExecute() {
dialog = ProgressDialog.show(context, "Buscando seu Produto","Por favor, espere um momento...",true ,false);
//I've already tried:
/*ProgressDialog dialog = new ProgressDialog(context);
dialog.setTitle("Buscando seu Produto");
dialog.setMessage("Por favor, espere um momento...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);*/
dialog.show();
super.onPreExecute();
}
@Override
protected String doInBackground(URL... params) {
try {
//Some WebService gets and Json conversions using my string variable
//and some Thread.sleep that counts 2000 miliseconds to do all the queries
dialog.dismiss();
} catch (IOException | InterruptedException |JSONException e) {
e.printStackTrace();
dialog.dismiss();
return e.getMessage();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
}
而且在我的活動我調用類RestClient
當我點擊這樣的按鈕:
---編輯:我忘了提,我有在AlertDialog可有時之前和之後ProgressDialog顯示此相同的活動---
private Button buttonConfirm;
private EditView evString;
private String theString;
private String returnFromExecute;
private RestClient restClient;
private AlertDialog.Builder dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_access_webservice);
evString = (EditText) findViewById(R.id.editViewMyString);
buttonConfirm = (Button) findViewById(R.id.buttonConfirm);
dialog = new ProgressDialog(IdentificacaoDeProdutoActivity.this);
dialog.setTitle("Error");
dialog.setMessage("Please try again");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
buttonConfirmar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
theString = evString.getText().toString();
if(!(theString!=null && theString.trim().length()>0)) //To check if theString is not null
{
dialog.show();
}
restClient = new RestClient(AccessWebserviceActivity.this, theString);
//Then I call execute and put a Thread.sleep a bit longer to compensate the ones I have in my doInBackground
restClient.execute();
try {
Thread.sleep(2050);
} catch (Exception e) {
dialog.show();
return;
}
}
}
}
的問題是,我從來沒有ProgressDialog
顯示。我已經試過getParent()
,getApplication()
和getApplicationContext()
而不是AccessWebserviceActivity.this
,但都沒有工作。有人知道發生了什麼,我該怎麼做?
你能告訴我你的AsyncTask類是你Activity類的子類還是AsnctaskClass有不同的包? – Hamza
@AmeerHamza,它不是我活動的孩子,因爲我在其他活動中使用它,但是它不在我的活動中。 – Cocholate