2

Asynctask に問題があります。OnPostExecute で doInBackground メソッドの戻り値を取得できません。助けてもらえますか?

Asynctask の戻り値を Integer として設定し、返される値は int であるのに、doInBackground の戻り値でアプリが停止するのはなぜですか?

Asynctask がどのように機能するかを説明していただければ、本当に感謝しています。

どうもありがとうございました!!

public class LoginActivity extends Activity 
{
ProgressDialog pDialog;
JSONParser jParser = new JSONParser();

Button btnLogin;
Button btnLinkToRegister;
EditText inputEmail;
EditText inputPassword;
TextView loginErrorMsg;

JSONArray user = null;
ArrayList<HashMap<String, String>> userList;


private static String loginURL = "htttp://example.com/query.php";

private static String KEY_SUCCESS = "success";
private static String KEY_EMAIL = "email";
private static String KEY_PASSWORD = "password";

@Override
public void onCreate(Bundle savedInstanceState) 
{    
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);

    userList = new ArrayList<HashMap<String, String>>();

    inputEmail = (EditText) findViewById(R.id.loginEmail);
    inputPassword = (EditText) findViewById(R.id.loginPassword);
    btnLogin = (Button) findViewById(R.id.btnLogin);
    btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);

    btnLogin.setOnClickListener(new View.OnClickListener() 
    {   
        @Override
        public void onClick(View v) 
        {
            new CheckUser().execute();          
        }
    });


}

/**
 * Background Async Task to Load all product by making HTTP Request
 * */

class CheckUser extends AsyncTask<String, String, Integer> 
{
    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() 
    {
        super.onPreExecute();
        pDialog = new ProgressDialog(LoginActivity.this);
        pDialog.setMessage("Verifica dati inseriti...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    protected Integer doInBackground(String... args)
    {
        int valoreOnPostExecute = 0;

        String emailInserted = inputEmail.getText().toString();
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("email", emailInserted));
        // getting JSON string from URL
        JSONObject json = null;

        json = jParser.getJSONFromUrl(loginURL, params);

        // Check your log cat for JSON response
        Log.d("All Users: ", json.toString());

        try 
        {
            // Checking for SUCCESS TAG
            int success = json.getInt(KEY_SUCCESS);

            if (success == 1) 
            {
                // users found
                // Getting Array of users
                user = json.getJSONArray("utenti");

                // looping through All users
                for (int i = 0; i < user.length(); i++) 
                {
                    JSONObject c = user.getJSONObject(i);

                    // Storing each json item in variable
                    String email = c.getString(KEY_EMAIL);
                    String password = c.getString(KEY_PASSWORD);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(KEY_EMAIL, email);
                    map.put(KEY_PASSWORD, password);

                    // adding HashList to ArrayList
                    userList.add(map);
                }

//                    Intent intent = new Intent(LoginActivity.this,GenereMusicale.class);
//                    startActivity(intent);
                    valoreOnPostExecute = success;
                } 
//                else 
//                {
//                  Log.d("success != 1", json.toString());
//                  //Toast.makeText(LoginActivity.this, "Username/password non corretti", Toast.LENGTH_SHORT).show();
//                }
        }
        catch (JSONException e) 
        {
            e.printStackTrace();
            Log.d("User ARRAY", "user array: "+user);
        }

        return valoreOnPostExecute;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(int valoreOnPostExecute) 
    {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        // updating UI from Background Thread

        if(valoreOnPostExecute == 1)
        {
            Intent intent = new Intent(LoginActivity.this,GenereMusicale.class);
            startActivity(intent);
        }
        else
        {
            new AlertDialog.Builder(LoginActivity.this)
            .setTitle("Fine del Round!")
            //.setMessage("Terminare round?")
            //.setNegativeButton(android.R.string.no, null)
            .setPositiveButton(android.R.string.ok, new OnClickListener() {

                public void onClick(DialogInterface arg0, int arg1) {
                    finish();
                }
            }).create().show();
        }
    }

}

}
4

1 に答える 1

7

あなたのリターンタイプ fromdoInBackground()と受信タイプ inonPostExecute()が一致しません

このようにしてみてください

class CheckUser extends AsyncTask<String, String, Integer> 

protected Integer doInBackground(String... args)

protected void onPostExecute(Integer valoreOnPostExecute) 
于 2012-11-17T11:09:47.687 に答える