Android 如果发生异常,如何从Catch块中删除意图
若发生异常,我正试图从catch块中删除intent。它并没有意图。当我输入错误的ip时,它会给出“malformedurlexception”。为此,我需要从其他活动更改我的ip地址。请帮助我 日志文件: 代码:Android 如果发生异常,如何从Catch块中删除意图,android,android-asynctask,try-catch,Android,Android Asynctask,Try Catch,若发生异常,我正试图从catch块中删除intent。它并没有意图。当我输入错误的ip时,它会给出“malformedurlexception”。为此,我需要从其他活动更改我的ip地址。请帮助我 日志文件: 代码: protected class AsyncLogin extends AsyncTask<String, JSONObject, Boolean> { String userName = null; @Override
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);
}
}
}
受保护类AsyncLogin扩展了AsyncTask{
字符串userName=null;
@凌驾
受保护的布尔doInBackground(字符串…参数){
RestAPI api=new RestAPI();
布尔userAuth=false;
试一试{
//调用API中的用户身份验证方法
JSONObject jsonObj=api.UserAuthentications(参数[0],
参数[1]);
JSONParser=新的JSONParser();
userAuth=parser.parseUserAuth(jsonObj);
用户名=参数[0];
//将JSON对象解析为布尔值
}捕获(例外e){
//TODO自动生成的捕捉块
Intent openStartingPoint=newintent(getApplicationContext(),AlertForIp.class);
openStartingPoint.putExtra(“Forcheck”、“Yes”);
启动触觉(打开启动点);
d(“AsyncLogin”,例如getMessage());
}
返回userAuth;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
@凌驾
受保护的void onPostExecute(布尔结果){
//TODO自动生成的方法存根
//检查用户有效性
如果(结果){
意向i=新意向(示例1),
主要活动(课堂);
i、 putExtra(“ForButton”,cnt);
i、 putExtra(“Uname”,用户名);
星触觉(i);
}否则{
Toast.makeText(上下文,“无效用户名/密码并重新输入IP”,Toast.LENGTH_SHORT.show();
意图i=新意图(Sample.this,AlertForIp.class);
i、 putExtra(“Forcheck”、“Yes”);
星触觉(i);
}
}
}
我认为异常应该与在api.UserAuthentications(params[0],params[1])中调用的url有关代码>
至于意图的发射,我将采用不同的方法。
在doInBackground中,在出现异常时,我会将“错误”状态作为结果传递给onPostExecute()
在onPostExecute中,将错误状态返回给调用活动
最后让调用活动在ui线程中启动新活动
进行快速测试更换
} 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());
}
与
通过返回false,onPostExecute()将接收false作为结果参数。根据“如果”这个词,它将和你在doInBackground的catch子句中做的一样
区别在于onPostExecute在UI线程上运行,而doInBackground不运行
如果有效,那么您可以在onPostExecute中将错误条件返回给调用活动。有解决方案吗?您可以添加错误日志吗?A什么是您的AlertForIp类?AlertForIp是输入Ip地址的活动我要求日志以了解异常引发的具体位置。它是在doInBackground中还是在onPostExecute中?并查看引发的确切异常。我添加了Image抱歉,但我没有得到。你能详细解释一下吗?我补充了一些澄清。
} catch (Exception e) {
// TODO Auto-generated catch block
Log.d("AsyncLogin", e.getMessage());
return false;
}