2013-04-11 37 views
1

我有一個probleme,我從JSON一文中,我分析它,但我需要從這個文本PIC的形象,這就是JSON文本:IMG從TextView的

"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\" /> 

,我想從這個文本PIC圖片與文字顯示出來,如果有一個人能幫助我,我不知道該怎麼做,我嘗試,但什麼也沒發生。

這是我收杆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; 
} 

,這是隻顯示文本。

+1

什麼問題?你知道內容的格式是什麼,只是解析它並獲取URL。如果您有特定問題,請發佈您的代碼。 – 2013-04-11 14:57:22

+0

是的,我解析它,但問題我不能顯示我解析文本,但圖片不是,我應該從文本分離img並顯示它與文本 – Sherlock 2013-04-11 15:03:00

+0

請顯示您的代碼 – 2013-04-11 15:04:35

回答

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

,我從這個文本要PIC的圖片與文字

目前使用的是其顯示:

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

Html.fromHtml()是非常基本的,我不不相信它支持從網絡上拉取圖片。但是,您可以使用XMLPullParser或SAX包中的DefaultHandler輕鬆解析<img>標記,然後將其顯示在ImageView,TextView或其他CompoundDrawable中。

+0

我不知道該怎麼做,我使用html.format只是爲了有一個超文本,因爲當我顯示文本,我看到我有這樣的東西「/%...」所以這就是爲什麼,但我不' t如何顯示img,:si嘗試模式和匹配器,但我不知道如何使用它們:s – Sherlock 2013-04-11 15:28:23

+0

@Kira如果你不怎麼使用某些東西,你應該嘗試學習和理解它,而不是要求別人爲你做這項工作。閱讀文檔,查找示例代碼,一次試驗一小塊 - 然後您就可以開始工作。當然,如果你更願意等待某人給你答案,那可能需要一段時間。 – 2013-04-11 15:36:22

+0

不,我不是等待某人,但我嘗試了很多次,我做了第一部分,但我不能做的第二部分,這是我的項目的最後一部分之一,這就是爲什麼我有麻煩。 – Sherlock 2013-04-11 15:44:42

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);  
     } 
    } 
+0

謝謝你的回答,但對於我發佈是合理的許多pures中的一個它返回一個列表中的內容列表,這是什麼使問題更加困難 – Sherlock 2013-04-11 15:42:50

0

當你提取字符串的一小部分,你可以逃脫使用正則表達式。

String imageSrc = ""; 

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

一旦你有你的字符串,有很多選項來顯示它 - 因爲你似乎可以用一個ListView,我建議你看看Universal Image Loader。它將異步下載和加載圖像。

通用圖像裝載機是非常簡單易用,看看下面這個例子:

imageLoader.displayImage(imageSrc, imageView); 

注:開發正則表達式時,我提到this優秀的StackOverflow問題。

+0

謝謝艾倫,我認爲這就是我要找的,我用通用圖像加載器已經爲另一部分,我會在這裏做只是需要知道如何分離img從textview – Sherlock 2013-04-11 15:46:51

+0

我剛剛更新了正則表達式,它應該現在完美工作! – Aaron 2013-04-11 16:19:31

+0

好的,謝謝,我會試着用這個原因,這是瘋狂的解決它:謝謝亞倫 – Sherlock 2013-04-11 16:29:27