在android上从null返回字符串

时间:2013-05-13 13:39:01

标签: android json

我有一个问题:我使用Web服务返回一些值,但有时候Web服务面临一些问题,当我想显示它们时我的应用程序崩溃,所以我想确保如果没有显示然后返回空白空间,我在JSON上使用它,如果有人可以帮助我,我的代码有一部分,

public static Article parseArticle(JSONObject jsonArticle) {

        Article article = new Article();

        try {
            article.setTitle(ArabicUtilities.reshape(Html.fromHtml(jsonArticle.getString("title")).toString()));
            article.setExcerpt(ArabicUtilities.reshape(Html.fromHtml(jsonArticle.getString("excerpt")).toString()));
            article.setContent(ArabicUtilities.reshape(Html.fromHtml(jsonArticle.getString("content")).toString()));
            article.setDate(jsonArticle.getString("date"));


            return article;
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }

private void loadinfo() {

         {
            programs = JSONParser.parseProgram(savedData);
            txt1.setText(article.get(1).getTitle());
            txt2.setText(article.get(0).getTitle());
            txt3.setText(article.get(1).excerpt());
            txt4.setText(article.get(0).excerpt());
            txt5.setText(article.get(1).content());
            txt6.setText(article.get(0).content());
            txt7.setText(article.get(1).date());
            txt8.setText(article.get(0).date());
        }

这是当我尝试在

上提供错误信息时发生的事情
        txt1.setText(article.get(1).getTitle());

没有值

2 个答案:

答案 0 :(得分:1)

如果我没有误解你而不是jsonArticle.getString,你可以使用jsonArticle.optString。如果json不包含该密钥,它将返回空String

String mString = jsonArticle.optString("title");
article.setTitle(ArabicUtilities.reshape(Html.fromHtml(mString).toString()));
mString = jsonArticle.optString("excerpt");                 
article.setExcerpt(ArabicUtilities.reshape(Html.fromHtml(mString).toString()));
mString = jsonArticle.optString("content");                     
article.setContent(ArabicUtilities.reshape(Html.fromHtml(mString).toString()));
mString = jsonArticle.optString("date");             
article.setDate(mString);

here optString()

的文档

修改

public static Article parseArticle(JSONObject jsonArticle) {

    Article article = new Article();

    try {
            String mString = jsonArticle.optString("title");
            article.setTitle(ArabicUtilities.reshape(Html.fromHtml(mString).toString()));
            mString = jsonArticle.optString("excerpt");                 
            article.setExcerpt(ArabicUtilities.reshape(Html.fromHtml(mString).toString()));
            mString = jsonArticle.optString("content");                     
            article.setContent(ArabicUtilities.reshape(Html.fromHtml(mString).toString()));
            mString = jsonArticle.optString("date");             
            article.setDate(mString);
    } catch (JSONException e) {
        e.printStackTrace();  
    }

       return article;
}

答案 1 :(得分:0)

public static Article parseArticle(JSONObject jsonArticle) {
     Article article = new Article();
    if(null!=jsonArticle){
        try {
            /*your code */
    }else{
        article.setTitle(ArabicUtilities.reshape("");
        article.setExcerpt(ArabicUtilities.reshape("");
        article.setContent(ArabicUtilities.reshape("");
        article.setDate("");
        return article;
        }
    }