1

i have a probleme , that i have a text from json , i parse it , but i need to pic image from this text, and this is the json text :

"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",

and we have into " content " the url

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

and i want from this text pic the picture and display it with the text , if some one can help me i don't know how to do it , i try but nothing happened .

this is my code of 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;
        }
    }

and this is my adapter

    @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;
}

and this is show just the text .

Sherlock
  • 19
  • 7
  • 1
    What's the problem? You know what the format of the content is, just parse it and get the URL. Post your code if you have specific issues. – Aleks G Apr 11 '13 at 14:57
  • yes i parse it but the probleme taht i can't display i parse just the text but the picture not , i should separet img from the text and show it with the text – Sherlock Apr 11 '13 at 15:03
  • Please show your code – Aleks G Apr 11 '13 at 15:04

3 Answers3

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

and i want from this text pic the picture and display it with the text

Currently you are using:

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

But Html.fromHtml() is very basic, I don't believe it supports pulling images from the web. However you can easily parse the <img> tag with an XMLPullParser or the DefaultHandler from the SAX package, then display this in a ImageView, TextView, or other CompoundDrawable.

Sam
  • 86,580
  • 20
  • 181
  • 179
  • i don't know how to do it , i use html.format just to have a hypertext because when i display the text i see that i have something like this" / % ... " so that's why , but i don't how to show img , :s i try with Pattern and Matcher but i didn't know how to use them :s – Sherlock Apr 11 '13 at 15:28
  • @Kira If you don't how to use something, you should attempt to learn and understand it rather than asking others to do the work for you. Read the documentation, find sample code, experiment with one small piece at a time - and you'll get it to work. Of course, if you prefer to wait for somebody to give you the answer, it may take quite some time. – Aleks G Apr 11 '13 at 15:36
  • no i'm not waiting from someone , but i try and try for manytimes , i did the first part but i can't did the seconde one this is one of last part for my project that's why i'm in trouble . – Sherlock Apr 11 '13 at 15:44
  • @Kira You can use Pattern, Matcher, or other forms of RegEx in this simple example, but they will fail for anything complex. (Read: [RegEx match open tags except XHTML self-contained tags](http://stackoverflow.com/q/1732348/1267661)) Also watch the Google I/O presentation [TurboCharge your UI](http://www.google.com/events/io/2009/sessions/TurboChargeUiAndroidFast.html) to learn why ListView's recycle the row layouts and how to take advantage of this feature. Back to your question about extracting `src`, if you post what you have tried I'm happy to help. – Sam Apr 11 '13 at 15:54
  • thatk you for your help i will look for those ones , thank you again Sam – Sherlock Apr 11 '13 at 15:56
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);    
        }
    }
ashakirov
  • 12,112
  • 6
  • 40
  • 40
  • thank you for your answer , but for what i post is juste one of a many pîcture it's return a list of content i made them on a listview , that's what make teh problem more difficult – Sherlock Apr 11 '13 at 15:42
0

As you're extracting a small part of the string, you can get away with using regex.

String imageSrc = "";

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

Once you have your string, there are many options to display it - as you seem to be using a ListView, I would recommend you check out Universal Image Loader. It will download and load images asynchronously.

Universal Image Loader is pretty simple to use, look at this example:

imageLoader.displayImage(imageSrc, imageView);

Note: I referred to this excellent StackOverFlow question when developing the regex pattern.

Community
  • 1
  • 1
Aaron
  • 699
  • 4
  • 14
  • thank you aaron, i think that's what i'm looking for , i used Universal Image Loader already for another part , i will do it here just need to know how to separete img from the textview – Sherlock Apr 11 '13 at 15:46
  • ok thank you i will try with this cause that's make crazy to solve it :s thank you Aaron – Sherlock Apr 11 '13 at 16:29
  • hello aaron , yes it wort but for patern first time i used what u give me , but didn't work with Pattern pattern = Pattern.compile("src=.\"(.*?)\""); so i changed to Pattern pattern = Pattern.compile("src=\"([^\"]+)\""); and it work :D and thank you – Sherlock Apr 25 '13 at 17:23