2016-07-18 2 views
0

Jungs dies Daten meiner json anzuzeigen:wie Bild-URL von JSON-String in Android bekommen in Bildansicht

{ 
    "status": "ok", 
    "post": { 
    "id": 8, 
    "type": "post", 
    "slug": "android", 
    "url": "http://192.168.1.8/Android/android/wordpress/2016/07/13/android/", 
    "status": "publish", 
    "title": "Android", 
    "title_plain": "Android", 
    "content": "<p>Google&#8217;s Android is shaking up the mobile market in a big way. With Android.</p>\n<p><img class=\"alignnone size-medium wp-image-31\" src=\"http://192.168.1.8/Android/android/wordpress/wp-content/uploads/2016/07/51KRggHZuzL._SX355_-300x222.jpg\" alt=\"51KRggHZuzL._SX355_\" width=\"300\" height=\"222\" srcset=\"http://192.168.1.8/Android/android/wordpress/wp-content/uploads/2016/07/51KRggHZuzL._SX355_-300x222.jpg 300w, http://192.168.1.8/Android/android/wordpress/wp-content/uploads/2016/07/51KRggHZuzL._SX355_.jpg 355w\" sizes=\"(max-width: 300px) 85vw, 300px\" /> <img class=\"alignnone size-medium wp-image-12\" src=\"http://192.168.1.8/Android/android/wordpress/wp-content/uploads/2016/07/CV2iAeMXIAAQlZH-300x232.png\" alt=\"moto\" width=\"300\" height=\"232\" srcset=\"http://192.168.1.8/Android/android/wordpress/wp-content/uploads/2016/07/CV2iAeMXIAAQlZH-300x232.png 300w, http://192.168.1.8/Android/android/wordpress/wp-content/uploads/2016/07/CV2iAeMXIAAQlZH.png 600w\" sizes=\"(max-width: 300px) 85vw, 300px\" /></p>\n", 
    "excerpt": "<p>Google&#8217;s Android is shaking up the mobile market in a big way. With Android.  </p>\n", 
    "date": "2016-07-13 06:12:00", 
    "modified": "2016-07-18 07:12:39" 
    } 
} 

In Inhaltsdaten Ich erhalte String wie folgt aus:

<p>Google&#8217;s Android is shaking up the mobile market in a big way. With Android.</p>\n<p><img class=\"alignnone size-medium wp-image-31\" src=\"http://192.168.1.8/Android/android/wordpress/wp-content/uploads/2016/07/51KRggHZuzL._SX355_-300x222.jpg\" alt=\"51KRggHZuzL._SX355_\" width=\"300\" height=\"222\" srcset=\"http://192.168.1.8/Android/android/wordpress/wp-content/uploads/2016/07/51KRggHZuzL._SX355_-300x222.jpg 300w, http://192.168.1.8/Android/android/wordpress/wp-content/uploads/2016/07/51KRggHZuzL._SX355_.jpg 355w\" sizes=\"(max-width: 300px) 85vw, 300px\" /> <img class=\"alignnone size-medium wp-image-12\" src=\"http://192.168.1.8/Android/android/wordpress/wp-content/uploads/2016/07/CV2iAeMXIAAQlZH-300x232.png\" alt=\"moto\" width=\"300\" height=\"232\" srcset=\"http://192.168.1.8/Android/android/wordpress/wp-content/uploads/2016/07/CV2iAeMXIAAQlZH-300x232.png 300w, http://192.168.1.8/Android/android/wordpress/wp-content/uploads/2016/07/CV2iAeMXIAAQlZH.png 600w\" sizes=\"(max-width: 300px) 85vw, 300px\" /></p>\n 

So , Wie bekomme ich Bild URL von diesem String

+0

ist es wirklich JSON? –

+0

Es ist String Form Json –

+0

Es ist HTML, nicht JSON. –

Antwort

1
Try this:: 

Document doc = Jsoup.parse(html); 
     Elements element = doc.getAllElements(); 
     for(Element e: element) 
     { 
      Elements str = e.getElementsByTag("img"); 
      for(Element el: str) 
      {     
       String src= el.attr("src"); 
       System.out.println("The src:"+src); 
      } 
     } 
+0

Was ist Jsoup? Kann Jsoup nicht auflösen. –

+0

Danke! Es funktioniert –

1

Sie können Regulärer Ausdruck verwenden, um das src Attribut von img Tag zu entsprechen.

diesen Code Versuchen Sie,

String mydata = "your html string"; 
String regex = "src\\s*=\\s*\"(.+?)\""; 

Pattern pattern = Pattern.compile(regex); 
Matcher matcher = pattern.matcher(mydata); 
List<String> imgUrls = new ArrayList<String>(); 
while (matcher.find()) { 
    imgUrls.add(matcher.group(1)); 
} 

//Load imgUrls into ImageViews using some image loader 

Für Imageloader können Sie Picasso oder Glide verwenden.

+0

Bitte verwenden Sie nicht Regex für HTML http://Stackoverflow.com/a/1732454/1483663 –

+0

[HTML-Parser] (https://jsoup.org/) wie von @Punithapriya vorgeschlagen –

+0

@ RuslanBes Irgendwelche Alternativen ohne irgendwelche externen Abhängigkeiten? –

0
URL[] urlsimage; 
Bitmap[] TBM; 

JSONArray jsonArray = new JSONArray(Response); 

urlsimage=new URL[jsonArray.length()]; 
TBM=new Bitmap[jsonArray.length()]; 

for(int i=0; i < jsonArray.length(); i++){ 
JSONObject jsonObject = jsonArray.getJSONObject(i); 

Image[i]=jsonObject.getString("image"); 
    if(!Image[i].equals("null")) 
    { 
     urlsimage[i]=new URL(Image[i]); 
     TBM[i] = BitmapFactory.decodeStream(urlsimage[i].openConnection().getInputStream()); 
    } 
} 

img.setImageBitmap(TBM[0]);