0

我有一个问题:我使用 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());

没有价值观

4

2 回答 2

1

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

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);

这里的文档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;
}
于 2013-05-13T13:59:18.600 回答
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;
        }
    }
于 2013-05-13T13:44:30.890 回答