2013-01-17 320 views
2

我正在開發一個簡單的應用程序,它將僞隨機字符串發送到服務器數據庫。在這裏,我使用AsyncTask將字符串發送到前臺的服務器和ProgressDialog。問題是進度對話框沒有停止,我不在服務器上輸入任何字符串。也許在發送srcetring的代碼中存在一些問題。我是Android新手,並從互聯網上的可用來源進行學習。這是我使用的代碼。ProgressDialog不停止與AsyncTask

public class MainActivity extends Activity { 

    Button btn; 
    TextView tv; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     tv = (TextView)findViewById(R.id.textView2); 
     btn = (Button)findViewById(R.id.button1); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

    public void getThis(View v) { 
     String str = "35" + 
      Build.BOARD.length()%10+ Build.BRAND.length()%10 + 
      Build.CPU_ABI.length()%10 + Build.DEVICE.length()%10 + 
      Build.DISPLAY.length()%10 + Build.HOST.length()%10 + 
      Build.ID.length()%10 + Build.MANUFACTURER.length()%10 + 
      Build.MODEL.length()%10 + Build.PRODUCT.length()%10 + 
      Build.TAGS.length()%10 + Build.TYPE.length()%10 + 
      Build.USER.length()%10 ; 
     tv.setText(str); 
     UploadUniqueID uni=new UploadUniqueID(this,str); 
     uni.execute(str); 
    } 

    class UploadUniqueID extends AsyncTask<String, Integer, String> { 

     Context context; 
     MainActivity ma; 
     ProgressDialog dialog; 
     String id; 
     public UploadUniqueID(MainActivity activity,String str) { 
      ma = activity; 
      context = activity; 
      dialog = new ProgressDialog(context); 
      id = str; 
     } 

     protected void onPreExecute() { 
      this.dialog.setMessage("Progress start"); 
      this.dialog.setCancelable(true); 
      this.dialog.show(); 
     } 

     @Override 
     protected String doInBackground(String... params) { 
      // perform long running operation operation 
      String id=params[0]; 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://10.0.2.2/UniqueIdApp/myPHP.php"); 

      try { 
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); 
       nameValuePairs.add(new BasicNameValuePair("android",id));  
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       httpclient.execute(httppost);   
      } catch (Exception e) { 
       Log.i("HTTP Failed", e.toString()); 
      }    

      return null; 
     } 

     protected void onProgressUpdate(Integer...integers) { 

     } 

     protected void onPostExecute(String...strings) { 
      tv.setText("Sent"); 
      if (dialog.isShowing()) { 
       dialog.dismiss(); 
      } 
     } 
    } 
} 
+0

如果您收到任何錯誤的logcat的帖子在這裏 –

+0

都能跟得上..我沒有得到任何錯誤.. – apatel

回答

0

您還可以創建並通過這樣做在你的UploadUniqueID的AsyncTask類初始化的對話框:

private final ProgressDialog dialog = new ProgressDialog(MainActivity.this); 

,只是關閉它在你的onPostExecute(...)方法,你是正在做。另外,將@Override註釋添加到您的onPreExecute()方法中。

+0

嘿它的工作..但它採取幾乎一分鐘完成該過程。這可能是什麼原因呢? – apatel

+0

@aakash它正在執行一個與網絡相關的任務,所以時間根據你試圖通過Web服務拉取多少數據而變化。然而,這是正常的,你可以做的是嘗試和優化PHP端的代碼,看看是否加快速度。 –

+0

Thanx兄弟..它幫了很多。 – apatel

0

我建議你來初始化protected void onCreate(Bundle savedInstanceState)方法你ProgressDialog dialog對象,並在onPostExecute(String...strings)方法關閉該進度條。可能是這會幫助你歡呼:)。

編輯

public class MainActivity extends Activity { 
ProgressDialog dialog; 
Button btn; 
TextView tv; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    tv = (TextView)findViewById(R.id.textView2); 
    btn = (Button)findViewById(R.id.button1); 
    dialog = new ProgressDialog(MainActivity.this); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

public void getThis(View v) { 
    String str = "35" + 
     Build.BOARD.length()%10+ Build.BRAND.length()%10 + 
     Build.CPU_ABI.length()%10 + Build.DEVICE.length()%10 + 
     Build.DISPLAY.length()%10 + Build.HOST.length()%10 + 
     Build.ID.length()%10 + Build.MANUFACTURER.length()%10 + 
     Build.MODEL.length()%10 + Build.PRODUCT.length()%10 + 
     Build.TAGS.length()%10 + Build.TYPE.length()%10 + 
     Build.USER.length()%10 ; 
    tv.setText(str); 
    UploadUniqueID uni=new UploadUniqueID(this,str); 
    uni.execute(str); 
} 

class UploadUniqueID extends AsyncTask<String, Integer, String> { 

    Context context; 
    MainActivity ma; 
    ProgressDialog dialog; 
    String id; 
    /*public UploadUniqueID(MainActivity activity,String str) { 
     ma = activity; 
     context = activity; 
     dialog = new ProgressDialog(context); 
     id = str; 
    }*/ 

    protected void onPreExecute() { 
     dialog.setMessage("Progress start"); 
     dialog.setCancelable(true); 
     dialog.show(); 
    } 

    @Override 
    protected String doInBackground(String... params) { 
     // perform long running operation operation 
     String id=params[0]; 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://10.0.2.2/UniqueIdApp/myPHP.php"); 

     try { 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); 
      nameValuePairs.add(new BasicNameValuePair("android",id));  
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      httpclient.execute(httppost);   
     } catch (Exception e) { 
      Log.i("HTTP Failed", e.toString()); 
     }    

     return null; 
    } 

    protected void onProgressUpdate(Integer...integers) { 

    } 

    protected void onPostExecute(String...strings) { 
     tv.setText("Sent"); 
     try{ 
    if(dialog!=null && dialog.isShowing()){ 
     dialog.setMessage("DownLoading finished"); 
     dialog.dismiss(); 
    } 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 
    } 
    } 
} 
+0

我試了一下。但不工作 – apatel

+0

有什麼你試過了嗎???你能顯示你嘗試過的代碼片段嗎? – BBdev

+0

我編輯了你的代碼現在看看它的工作與否? – BBdev

0

試試下面的代碼:

public class MainActivity extends Activity { 
    Button btn; 
    TextView tv; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     tv = (TextView) findViewById(R.id.textView1); 
     btn = (Button) findViewById(R.id.button1); 
    } 

    public void getThis(View v) { 
     String str = "35" + Build.BOARD.length() % 10 + Build.BRAND.length() 
      % 10 + Build.CPU_ABI.length() % 10 + Build.DEVICE.length() % 10 
      + Build.DISPLAY.length() % 10 + Build.HOST.length() % 10 
      + Build.ID.length() % 10 + Build.MANUFACTURER.length() % 10 
      + Build.MODEL.length() % 10 + Build.PRODUCT.length() % 10 
      + Build.TAGS.length() % 10 + Build.TYPE.length() % 10 
      + Build.USER.length() % 10; 
     tv.setText(str); 
     UploadUniqueID uni = new UploadUniqueID(this, str); 
     uni.execute(str); 
    } 

    class UploadUniqueID extends AsyncTask<String, Integer, String> { 
     Context context; 
     MainActivity ma; 
     ProgressDialog dialog; 
     String id; 

     public UploadUniqueID(MainActivity activity, String str) { 
      ma = activity; 
      context = activity; 
      dialog = new ProgressDialog(activity); 
      id = str; 
     } 

     protected void onPreExecute() { 
      dialog.setMessage("Progress start"); 
      dialog.setCancelable(true); 
      dialog.show(); 
     } 

     @Override 
     protected String doInBackground(String... params) { 
      // perform long running operation operation 
      String id = params[0]; 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(
       "http://10.0.2.2/UniqueIdApp/myPHP.php"); 
      try { 
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
        1); 
       nameValuePairs.add(new BasicNameValuePair("android", id)); 
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       httpclient.execute(httppost); 
      } catch (Exception e) { 
       Log.i("HTTP Failed", e.toString()); 
      } 

      return null; 
     } 

     protected void onProgressUpdate(Integer... integers) { } 

     protected void onPostExecute(String p_result) { 
      super.onPostExecute(p_result);   
      tv.setText("Sent"); 
      if (dialog.isShowing()) { 
       dialog.dismiss(); 
      } 
     } 
    } 
} 
+0

親愛的,只是註解,這是用來告訴當我們想要重寫一個方法時,我們可以使用這個註釋來告訴編譯器我們正在覆蓋現有的方法。如果編譯器發現在超類中找不到匹配的方法,則會生成警告。當我們重寫一個方法時,這不是強制使用@Override。雖然它不是強制性的,但它被認爲是最佳實踐。 – BBdev

+0

@BBdev - 是的,我知道它不是強制性的,但如果你想關閉進度對話框,那麼你必須調用它。否則編譯器將無法檢測到它,並且不會調用'dialog.dismiss();'。我發現只有這個問題,它的'onPostExecute'沒有被叫做那個爲什麼我提出這個解決方案。 – GrIsHu

+1

這是嗎?讓我如果從'oncreate'方法中刪除@Override並運行我的應用程序,你認爲它不會啓動我的應用程序?試試吧。據我所知,它會運行你的應用程序。 – BBdev