2013-06-26 20 views
0

我有這樣的代碼: 類AttemptLogin延伸的AsyncTask {你如何設置定時ProgressDialog?

@Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(MyAkan.this); 
     pDialog.setMessage("Attempting login..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(true); 
     pDialog.show(); 
    } 

    @Override 
    protected String doInBackground(String... arg0) { 
     int success; 
     String idnumber = etIdnumber.getText().toString(); 
     String password = etPassword.getText().toString(); 

     try { 
      // Building Parameters 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      params.add(new BasicNameValuePair("idnumber", idnumber)); 
      params.add(new BasicNameValuePair("password", password)); 

      JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST", 
        params); 

      success = json.getInt(TAG_SUCCESS); 
      if (success == 1) { 
       Log.d("Login Successful!", json.toString()); 

       Intent i = new Intent("com.gpplsmje.mac.akan.AKANMENU"); 
       String id = json.getString(TAG_IDNUMBER); 
       i.putExtra("idnumber", id); 
       finish(); 
       startActivity(i); 
       return json.getString(TAG_MESSAGE); 
      } else if (success == 0) { 
       Log.d("Login Failure!", json.getString(TAG_MESSAGE)); 
       return json.getString(TAG_MESSAGE); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(String file_url) { 
     // TODO Auto-generated method stub 
     pDialog.dismiss(); 
     if (file_url != null) { 
      Toast.makeText(MyAkan.this, file_url, Toast.LENGTH_LONG).show(); 
     } 
    } 

} 

此應用連接到服務器和編碼的JSON數據的回報。每當我在我的機器上的本地服務器上測試它時,它都能正常工作。它與服務器連接良好。但是,當我關閉服務器並使用應用程序時,應用程序會加載,但在登錄嘗試一分鐘後停止。有沒有辦法讓進度最多20秒,然後如果它消耗了時間,它必須關閉進度對話框並返回主要活動?

謝謝。

回答

0

您的jsonParser對象使用HTTPURLConnection來執行http請求嗎?如果是的話有一個方法setConnectTimeout

如果您正在使用DefaultHttpClient看看一個這個問題How to set HttpResponse timeout for Android in Java

記住的Android 2.2以上,最好使用HttpURLConnection的看一看http://android-developers.blogspot.fr/2011/09/androids-http-clients.html瞭解更多詳情。

+0

我沒有使用HTTPURLConnection。我只是使用DefaultHttpClient和HttpPost進行連接。 – Paul

+0

看看這個答案http://stackoverflow.com/questions/693997/how-to-set-httpresponse-timeout-for-android-in-java – JEY

+0

我已經看過鏈接並將其添加到我的jsonParser碼。它不會工作。在使用HttpGet的鏈接中,它是否可以與HttpPost一起使用?這就是我現在使用的。我仍然在使用HTTPURLConnection。 Thankz。 – Paul

相關問題