我是android中的新手,所以我对所有错误并不吝啬是我得到的错误(JSONException的无法访问的catch块。这个异常永远不会从try语句体中抛出)在我的 catch 子句。任何人都可以告诉我该怎么办。谢谢我的代码:
if (usernameEditText == null || passwordEditText == null)
{
Toast.makeText(HelloAndroid.this, "Please enter your username & password",Toast.LENGTH_SHORT).show();
}
else
{
// display the username and the password in string format
try
{
showBusyCursor(true);
progress = ProgressDialog.show(this,"Please wait...", "Login in process", true);
Log.i(DEB_TAG, "Username: " + sUserName + "nPassword: " + sPassword);
Log.i(DEB_TAG, "Requesting to "+address);
JSONObject json = RestJsonClient.connect(address);
}
catch (JSONException e)
{
e.printStackTrace();
showBusyCursor(false);
}
}
答案 0 :(得分:1)
您的错误意味着,那
1。 try
块中的代码永远不会引发JSONException
,
2。或JSONException
在catch (JSONException e)
阻止之前被捕获,因此您的try-catch
阻止可能如下所示:
try {
[...]
}
catch (Exception e) {
// some code
}
catch (JSONException e) {
//some other code...
}
此处catch (Exception e)
块在JSONException
之前被调用,并且由于JSONException
扩展了Exception
类,因此不会再输入它。
在这种情况下,您应该更改catch块的顺序,并且能够以不同的方式处理这两种异常类型。
答案 1 :(得分:0)
使用此链接可以更好地了解如何在android中进行JSON解析:
http://www.androidcompetencycenter.com/2009/10/json-parsing-in-android/
另外提供您正在获取的错误日志以及您正在使用的代码。没有错误日志和代码,没有人不能说出错误。
答案 2 :(得分:0)
我在这里解析了一个JSON字符串的完整try catch实现:http://pastebin.com/5ZYw5wND