1

我有一个问题,我有一个来自 json 的文本,我解析它,但我需要从这个文本中获取图片,这是 json 文本:

"posts": [
{
"id": 22201,
"type": "post",
"slug": "basket-le-wac-simpose-65-a-42-face-au-cmc",
"url": "http://www.wydadnews.com/?p=22201",
"status": "publish",
"title": "Basket: Le WAC s’impose 65 à 42 face au CMC",
"title_plain": "Basket: Le WAC s’impose 65 à 42 face au CMC",
"content": "<p><img alt=\"null\" src=\"http://dl.dropboxusercontent.com/u/60787184/basket8373.jpg\" align=\"left\" />Le Wydad a battu, ce soir, le CMC 65 à 42 en match comptant pour la 8e journée du championnat. Les Wydadis enchainent ainsi leur 7e victoire consécutive en championnat, sur 7 matchs disputés.  <a href=\"http://www.wydadnews.com/?p=22201#more-22201\" class=\"more-link\">Read more</a></p>\n",
"excerpt": "Le Wydad a battu, ce soir, le CMC 65 à 42 en match comptant pour la 8e journée du championnat. Les Wydadis enchainent ainsi leur 7e victoire consécutive en championnat, sur 7 matchs disputés.",
"date": "2013-04-10 21:38:41",
"modified": "2013-04-11 11:23:26",

我们已经进入“内容”的网址

<p><img alt=\"null\" src=\"http://dl.dropboxusercontent.com/u/60787184/basket8373.jpg\" align=\"left\" />

我想从这个文本图片中显示图片并与文本一起显示,如果有人可以帮助我,我不知道该怎么做,我尝试但没有任何反应。

这是我的 pars 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;
        }
    }

    public static ArrayList<Article> parseArticles(String str) {
        JSONObject json = null;
        ArrayList<Article> list = new ArrayList<Article>();
        if (str == null)
            return list;

        try {
            JSONArray array;
            json = new JSONObject(str);
            array = json.getJSONArray("posts");

            for (int i = 0; i < array.length(); i++) {
                Article node = parseArticle(array.getJSONObject(i));
                list.add(node);

            }

            return list;

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

这是我的适配器

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) 

getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.list_item, null);

    TextView txtArticleTitle = (TextView) view.findViewById(R.textviews.txtArticleTitle);

    TextView txtArticleExcerpt = (TextView) view.findViewById(R.textviews.txtArticleExcerpt);
    TextView txtArticleDate = (TextView) view.findViewById(R.id.textArticleDate);


    Article article = getItem(position);

    txtArticleTitle.setText(ArabicUtilities.reshape(article.getTitle()));
    txtArticleExcerpt.setText(ArabicUtilities.reshape(article.getExcerpt()));
    txtArticleDate.setText(article.getDate().toGMTString());

    return view;
}

这只是显示文本。

4

3 回答 3

0
<img alt=\"null\" src=\"http://dl.dropboxusercontent.com/u/60787184/basket8373.jpg\" align=\"left\" />

我想从这个文本图片中获取图片并与文本一起显示

目前您正在使用:

article.setContent(ArabicUtilities.reshape(Html.fromHtml(
        jsonArticle.getString("content")).toString()));

但是Html.fromHtml()非常基础,我不相信它支持从网络上提取图像。<img>但是,您可以使用 XMLPullParser 或 SAX 包中的 DefaultHandler轻松解析标记,然后将其显示在 ImageView、TextView 或其他 CompoundDrawable 中。

于 2013-04-11T15:20:27.627 回答
0

当您提取字符串的一小部分时,您可以使用正则表达式。

String imageSrc = "";

Pattern pattern = Pattern.compile("src=.\"(.*?)\"");
Matcher matcher = pattern.matcher(str);
if (matcher.find()) {
    imageSrc = matcher.group(1);
}

一旦你有了你的字符串,就有很多选项可以显示它——因为你似乎在使用 ListView,我建议你查看Universal Image Loader。它将异步下载和加载图像。

Universal Image Loader 使用起来非常简单,看看这个例子:

imageLoader.displayImage(imageSrc, imageView);

注意:我在开发 regex 模式时提到了这个优秀的 StackOverFlow 问题。

于 2013-04-11T15:41:35.477 回答
0
    //your string
    String yourString="<p><img alt=\"null\" src=\"http://dl.dropboxusercontent.com/u/60787184/basket8373.jpg\" align=\"left\" />Le Wydad a battu, ce soir, le CMC 65 à 42 en match comptant pour la 8e journée du championnat. Les Wydadis enchainent ainsi leur 7e victoire consécutive en championnat, sur 7 matchs disputés.  <a href=\"http://www.wydadnews.com/?p=22201#more-22201\" class=\"more-link\">Read more</a></p>\n"; 

    //space is divider so
    String[] stringsArray = yourString.split(" ");

    for (String item : stringsArray) {
        if(item.startsWith("src=")){

            //here is your picture url
            String url = item.replace("src=\"", "").replaceAll("\"", "");
            System.out.println(url);    
        }
    }
于 2013-04-11T15:35:40.607 回答