我正在检查System.out.println(“true”);在doInBackground中,它返回true。但是之后onPostExecute()没有启动。我是初学者。这是代码:
@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 :(得分: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();
}
}
}
亲切的问候,