2017-01-17 15 views
1

我有一個名爲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,但都沒有工作。有人知道發生了什麼,我該怎麼做?

+0

你能告訴我你的AsyncTask類是你Activity類的子類還是AsnctaskClass有不同的包? – Hamza

+0

@AmeerHamza,它不是我活動的孩子,因爲我在其他活動中使用它,但是它不在我的活動中。 – Cocholate

回答

1

了很多關於線程和進程的研究中我發現,我有我的 在

new Thread(new Runnable() { public void run() { // My code } });

RestClient.execute使代碼的執行發生後封裝所有我的代碼後在後臺以及WebService查詢中。

編輯:

即使創建一個新的線程的作品,它不建議!正確的做法是創建另一個類來擴展AsyncTask以完成工作。

1

你還沒有創建進度對話框試試這個。

ProgressDialog dialog; 

@Override 
    protected void onPreExecute() { 
     dialog= new ProgressDialog(context); 
     dialog.setMessage("on Progress"); 
     dialog.show(); 
     super.onPreExecute(); 
    } 
+0

請不要否定我的問題。不是那麼簡單。無論如何,這個進度條根本不會出現。 – Cocholate

+0

我沒有做錯任何事我很驚訝爲什麼我的答案不適合你。 –

+0

沒關係。我已經嘗試了每一個博客和每個解決方案所說的一切,但它並不奏效。 – Cocholate

0

你需要調用

dialog = ProgressDialog.show(context, "Buscando seu Produto","Por favor, espere um momento...",true ,false); 

,並刪除

dialog.show(); 

也把你的dialog.dismiss();方法在onPostExecute()中。這個dialog.dismiss()方法在catch塊中很好,但是如果你在try塊中調用這個方法,它的目的是什麼。只要您撥打AsyncTask,它將立即刪除進度對話框。

+0

您是否已經從doInBackground方法中的try塊中移除了解僱方法?如果您收到任何錯誤,請將其粘貼,以便有人可以幫助您。 – Raheel

+0

是的,我有。我只是把它放在那裏,因爲某個地方說這可能是問題之一,但沒有奏效, – Cocholate

+0

我得到一個I /編舞:跳過了126幀!該應用程序可能在其主線程上做了太多工作。「 – Cocholate

0
returnFromExecute = restClient.get(); 

刪除該語句。您已經:

restClient.execute(); 

應該這樣做。

doInBackground()的結果您應該在onPostExecute()中處理。它不能在onCreate()中處理或檢索。

相關問題