I am trying to intent from catch block if exception is occurs.It does not intent. When I have enter wrong ip then it gives "malformedurlexception" For that purpose I need to change my ip address from another activity please help me.
The log file:
The code:
protected class AsyncLogin extends AsyncTask<String, JSONObject, Boolean> {
String userName = null;
@Override
protected Boolean doInBackground(String... params) {
RestAPI api = new RestAPI();
boolean userAuth = false;
try {
// Call the User Authentication Method in API
JSONObject jsonObj = api.UserAuthentications(params[0],
params[1]);
JSONParser parser = new JSONParser();
userAuth = parser.parseUserAuth(jsonObj);
userName = params[0];
//Parse the JSON Object to boolean
} catch (Exception e) {
// TODO Auto-generated catch block
Intent openStartingPoint = new Intent(getApplicationContext(), AlertForIp.class);
openStartingPoint.putExtra("Forcheck", "Yes");
startActivity(openStartingPoint);
Log.d("AsyncLogin", e.getMessage());
}
return userAuth;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(Boolean result) {
// TODO Auto-generated method stub
//Check user validity
if (result) {
Intent i = new Intent(Sample.this,
MainActivity.class);
i.putExtra("ForButton", cnt);
i.putExtra("Uname", userName);
startActivity(i);
} else {
Toast.makeText(context, "Not valid username/password and Re-enter IP", Toast.LENGTH_SHORT).show();
Intent i = new Intent(Sample.this, AlertForIp.class);
i.putExtra("Forcheck", "Yes");
startActivity(i);
}
}
}
I think the exception should be related to url being called in api.UserAuthentications(params[0], params[1]);
As of the firing of the intent, I would follow a different approach. In doInBackground, upon exception, I would pass an "error" status as result to onPostExecute().
And in onPostExecute, return the error status to the calling activity.
And finally have the calling activity, in the ui thread, start the new activity.
For a quick test replace
} catch (Exception e) {
// TODO Auto-generated catch block
Intent openStartingPoint = new Intent(getApplicationContext(), AlertForIp.class);
openStartingPoint.putExtra("Forcheck", "Yes");
startActivity(openStartingPoint);
Log.d("AsyncLogin", e.getMessage());
}
with
} catch (Exception e) {
// TODO Auto-generated catch block
Log.d("AsyncLogin", e.getMessage());
return false;
}
By returning false, onPostExecute() will receive false as the result parameter. And acording to the "if" it will do the same you are doing in the catch clause of doInBackground.
The difference is that onPostExecute runs on the UI thread and doInBackground doesn't.
If it works, then you can work on the part of returning the error condition to the calling activity in the onPostExecute.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.