我有一个Asynctask的问题,我无法在OnPostExecute中获取doInBackground方法的返回值,你能帮助我吗?
我将Asynctask的返回值设置为Integer,并且我返回的值是一个int,那么为什么app会在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();
}
}
}
}
答案 0 :(得分:7)
doInBackground()
的回复类型与onPostExecute()
中的接收类型不匹配
试试这个
class CheckUser extends AsyncTask<String, String, Integer>
protected Integer doInBackground(String... args)
protected void onPostExecute(Integer valoreOnPostExecute)