处理异常
我为网络服务编写了一个客户端。就像,确实请求了 ->得到了 JSON 答案。 但如果用户未经授权,我会得到“错误”的 JSON
答案和 NullPointerException
。此外,逻辑是根据 JSON 答案为 ListView
创建 ListAdapter
。但如果 JSON
null 那么应用程序将会崩溃。
我想像这样处理异常:
获取适配器:
public mListAdapter getData(String api){
mListAdapter result = null;
MyData[][] mData = null;
try{
Gson gson = new Gson();
Reader r = new InputStreamReader(getJSONData(api));
// getJSONData, here i can get Exception and return null, but i read what control of logic via exception is bad practice
}catch(Exception ex){
ex.printStackTrace();
Log.i("NullPointerException", "error");
return result;
}
result = new mListAdapter(title, mData);
return result;
}
getJSONData:
public InputStream getJSONData(String url) throws NullPointerException{
InputStream data = null;
try {
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
data = response.getEntity().getContent();
//if (response.getEntity().getContent().getStatus()==404) return null, for example
} catch (Exception e) {
e.printStackTrace();
return null;
}
return data;
}
并设置 Adapter
to ListView
:
if (adapter!=null) IWatchList.setAdapter(adapter); else IWatchList.setEmptyView(new TextView(this));
我不喜欢这种方法,因为:
- 我想出来的。
- 前两种方法通用,因此每个适配器都需要像第三种方法一样描述,这不太舒服。另一方面如何定义适配器是否为空!只要检查一下即可。
请说明您的优点和缺点、建议和建议,以及如何更好以及为什么。
I write a client for web service. Like, did request -> got JSON
answer.
But if user not authorized I got "bad" JSON
answer and NullPointerException
. Further, the logic is create ListAdapter
for a ListView
from JSON answer. But if JSON
null then app will crash.
I want do like this for handle exception:
Get Adapter:
public mListAdapter getData(String api){
mListAdapter result = null;
MyData[][] mData = null;
try{
Gson gson = new Gson();
Reader r = new InputStreamReader(getJSONData(api));
// getJSONData, here i can get Exception and return null, but i read what control of logic via exception is bad practice
}catch(Exception ex){
ex.printStackTrace();
Log.i("NullPointerException", "error");
return result;
}
result = new mListAdapter(title, mData);
return result;
}
getJSONData:
public InputStream getJSONData(String url) throws NullPointerException{
InputStream data = null;
try {
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
data = response.getEntity().getContent();
//if (response.getEntity().getContent().getStatus()==404) return null, for example
} catch (Exception e) {
e.printStackTrace();
return null;
}
return data;
}
And set Adapter
to ListView
:
if (adapter!=null) IWatchList.setAdapter(adapter); else IWatchList.setEmptyView(new TextView(this));
I don't like this method, because:
- I came up it.
- First two methods universal, therefore each adapter need describe like third method, this is not comfortably. On the other hand how else to define the adapter is empty or not! Only if check it.
Please, your pros and cons, proposal and tips how better and why.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论