处理异常

发布于 2024-12-11 12:24:06 字数 1717 浏览 0 评论 0原文

我为网络服务编写了一个客户端。就像,确实请求了 ->得到了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文