2013-09-22 56 views
0

我正在使用System.out.println(「true」)進行檢查;在doInBackground中,它返回true。但onPostExecute()不會在此之後開始。我是這個初學者。以下是代碼:onPostExecute()不會在doInBackground後啓動

@SuppressWarnings("rawtypes") 
private class NetCheck extends AsyncTask { 
    private ProgressDialog nDialog; 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     nDialog = new ProgressDialog(Register.this); 
     nDialog.setMessage("Loading.."); 
     nDialog.setTitle("Checking Network"); 
     nDialog.setIndeterminate(false); 
     nDialog.setCancelable(true); 
     nDialog.show(); 
    } 

    protected Object doInBackground(Object... args) { 

     /** 
     * Gets current device state and checks for working internet 
     * connection by trying Google. 
     **/ 

     ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo netInfo = cm.getActiveNetworkInfo(); 
     if (netInfo != null && netInfo.isConnected()) { 
      System.out.println("connected"); 
      try { 
       URL url = new URL("http://www.google.com"); 
       HttpURLConnection urlc = (HttpURLConnection) url 
         .openConnection(); 
       urlc.setConnectTimeout(3000); 
       urlc.connect(); 
       if (urlc.getResponseCode() == 200) { 
        System.out.println("true"); 
        return true; 
       } 
      } catch (MalformedURLException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     System.out.println("Do in background"); 
     return false; 

    } 

    @SuppressWarnings({ "unchecked", "unused" }) 
    protected void onPostExecute(Boolean th) { 
     System.out.println("on post execute"); 
     if (th == true) { 
      nDialog.dismiss(); 
      new ProcessRegister().execute(); 
     } else { 
      nDialog.dismiss(); 
      Toast.makeText(getApplicationContext(), 
        "Error in Network Connection", Toast.LENGTH_LONG) 
        .show(); 
     } 
    } 
} 

@SuppressWarnings("rawtypes") 
private class ProcessRegister extends AsyncTask { 

    /** 
    * Defining Process dialog 
    **/ 
    private ProgressDialog pDialog; 

    String email, password, name, password1; 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     inputPassword = (EditText) findViewById(R.id.etRegisterPassword); 
     inputPassword1 = (EditText) findViewById(R.id.etRegisterConfirmPassword); 
     email = inputEmail.getText().toString(); 
     name = inputName.getText().toString(); 
     password = inputPassword.getText().toString(); 
     password1 = inputPassword.getText().toString(); 
     pDialog = new ProgressDialog(Register.this); 
     pDialog.setTitle("Contacting Servers"); 
     pDialog.setMessage("Registering ..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(true); 
     pDialog.show(); 
    } 

    @Override 
    protected void onProgressUpdate(Object... values) { 
     // TODO Auto-generated method stub 
     super.onProgressUpdate(values); 
     System.out.println("on progressss"); 
    } 

    protected Object doInBackground(Object... args) { 

     UserFunctions userFunction = new UserFunctions(); 
     JSONObject json = userFunction.registerUser(name, email, password); 

     return (JSONObject) json; 

    } 

    @SuppressWarnings("unused") 
    protected void onPostExecute(JSONObject json) { 
     /** 
     * Checks for success message. 
     **/ 
     System.out.println("on posttt execute"); 
     try { 
      if (json.getString(KEY_SUCCESS) != null) { 
       String res = json.getString(KEY_SUCCESS); 

       String red = json.getString(KEY_ERROR); 

       if (Integer.parseInt(res) == 1) { 
        pDialog.setTitle("Getting Data"); 
        pDialog.setMessage("Loading Info"); 

        registerErrorMsg.setText("Successfully Registered"); 

        DatabaseHandler db = new DatabaseHandler(
          getApplicationContext()); 
        JSONObject json_user = json.getJSONObject("user"); 

        /** 
        * Removes all the previous data in the SQlite database 
        **/ 

        UserFunctions logout = new UserFunctions(); 
        logout.logoutUser(getApplicationContext()); 
        db.addUser(json_user.getString(KEY_NAME), 
          json_user.getString(KEY_EMAIL), 
          json_user.getString(KEY_UID), 
          json_user.getString(KEY_CREATED_AT)); 
        /** 
        * Stores registered data in SQlite Database Launch 
        * Registered screen 
        **/ 

        Intent registered = new Intent(getApplicationContext(), 
          LogIn.class); 

        /** 
        * Close all views before launching Registered screen 
        **/ 
        registered.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        pDialog.dismiss(); 
        startActivity(registered); 

        finish(); 
       } 

       else if (Integer.parseInt(red) == 2) { 
        pDialog.dismiss(); 
        Toast.makeText(getApplicationContext(), 
          "User already exists", Toast.LENGTH_LONG) 
          .show(); 
       } else if (Integer.parseInt(red) == 3) { 
        pDialog.dismiss(); 
        Toast.makeText(getApplicationContext(), 
          "Invalid Email id", Toast.LENGTH_LONG).show(); 
       } 

      } 

      else { 
       pDialog.dismiss(); 
       Toast.makeText(getApplicationContext(), 
         "Error occured in registration", Toast.LENGTH_LONG) 
         .show(); 
      } 

     } catch (JSONException e) { 
      e.printStackTrace(); 

     } 
    } 
} 

@SuppressWarnings("unchecked") 
public void NetAsync(View view) { 
    new NetCheck().execute(); 
}} 

我在其他有關此問題的答案中找不到答案。

回答

0

這裏是一個快速的解決辦法:使用protected void onPostExecute(Object th) {代替protected void onPostExecute(Boolean th) {再投日在行布爾:if (th == true) {,即:if ((Boolean)th == true) {

這是快速修復,但我建議您使用更精確的類型並處理所有壓制警告,它們在您的代碼中不是一件好事。他們只是隱藏了許多其他潛在的錯誤來源。

這裏是一個更乾淨的代碼,你可能只是複製和過去:

private class NetCheck extends AsyncTask<Void, Void, Boolean> { 
    private ProgressDialog nDialog; 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     nDialog = new ProgressDialog(Register.this); 
     nDialog.setMessage("Loading.."); 
     nDialog.setTitle("Checking Network"); 
     nDialog.setIndeterminate(false); 
     nDialog.setCancelable(true); 
     nDialog.show(); 
    } 

    @Override 
    protected Boolean doInBackground(Void... args) { 

     /** 
     * Gets current device state and checks for working internet 
     * connection by trying Google. 
     **/ 

     ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo netInfo = cm.getActiveNetworkInfo(); 
     if (netInfo != null && netInfo.isConnected()) { 
      System.out.println("connected"); 
      try { 
       URL url = new URL("http://www.google.com"); 
       HttpURLConnection urlc = (HttpURLConnection) url 
         .openConnection(); 
       urlc.setConnectTimeout(3000); 
       urlc.connect(); 
       if (urlc.getResponseCode() == 200) { 
        System.out.println("true"); 
        return true; 
       } 
      } catch (MalformedURLException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     System.out.println("Do in background"); 
     return false; 

    } 

    @Override 
    protected void onPostExecute(Boolean th) { 
     System.out.println("on post execute"); 
     if (th == true) { 
      nDialog.dismiss(); 
      new ProcessRegister().execute(); 
     } else { 
      nDialog.dismiss(); 
      Toast.makeText(getApplicationContext(), 
        "Error in Network Connection", Toast.LENGTH_LONG) 
        .show(); 
     } 
    } 
} 

親切的問候,

相關問題