1

Image

I'm parsing this content from Wordpress blog. I don't know how to get Images from JSON. This is the image url "content":"<p><img class=\"aligncenter\" style=\"cursor: -moz-zoom-in;\" src=\"http:\/\/sphotos-h.ak.fbcdn.net\/hphotos-ak-ash4\/395050_10151219612828815_5123523_n.jpg\" alt=\"http:\/\/sphotos-h.ak.fbcdn.net\/hphotos-ak-ash4\/395050_10151219612828815_5123523_n.jpg\" width=\"390\" height=\"466\" \/><\/p>\n<p><span id=\"more-5267\"><\/span><\/p>\n<p>Some texts here...XXXXXYYYYYZZZZ"

HttpClient client;
HttpGet get;
HttpResponse res;
HttpEntity ent;
Button b;
TextView tv1,tv2,tv3;
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Typeface tp=Typeface.createFromAsset(getAssets(), "AftaSansThin-Regular.otf");
    StrictMode.enableDefaults();
    b = (Button) findViewById(R.id.button1);
    tv1 = (TextView) findViewById(R.id.textView1);
    tv2 = (TextView) findViewById(R.id.textView2);
    tv3 = (TextView) findViewById(R.id.textView3);
    tv1.setTypeface(tp);
    tv2.setTypeface(tp);
    tv3.setTypeface(tp);
    client = new DefaultHttpClient();
    get = new HttpGet("http://example.com");
    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            try {
                res=client.execute(get);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            ent=res.getEntity();
            InputStream is = null;
            try {
                is=ent.getContent();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            StringBuffer sb = new StringBuffer();
            String line = null;
            do{
                try {
                    line = br.readLine();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                sb.append(line);
            } while (line!=null);
            String str = sb.toString();

            try {
                JSONObject ob1 = new JSONObject(str);
                JSONObject ob2 = ob1.getJSONObject("post");
                String title = ob2.getString("title");
                String date = ob2.getString("date");
                String content = ob2.getString("content");



                tv1.setText(title);
                tv2.setText(date);
                Spanned marked_up = Html.fromHtml(content);
                tv3.setText(marked_up.toString(),BufferType.SPANNABLE);

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
}
3
  • try to get only the image url to do the downloading from the json string, that way you can plug it in an asynctask and download all the images Commented Dec 14, 2012 at 3:10
  • @GreyBeardedGeek check code! Commented Dec 14, 2012 at 3:38
  • Post your data,so we can help you to extract it Commented Aug 27, 2013 at 9:27

4 Answers 4

1

First of all that's not a valid image Url - \"http://sphotos-h.ak.fbcdn.net/hphotos-ak-ash4/395050_10151219612828815_5123523_n.jpg\" is closer to what you want but still has all the escape characters.

If you can get the JSON packet to send down the URL as a string that would be much more efficient. If not configure it so that it doesn't escape all the characters then strip out the Url from the returned text.

Next, what I would do is to start a http connection in an AsyncTask and acquire an input stream. Use BitmapFactory.decodeStream(HttpInputStream) to turn the stream into a bitmap and then return the bitmap to the UI thread onPostExecute.

If you are dealing with many image downloads you might want to consider an open source library or else writing an AsyncImageLoader

hope this helps :)

Sign up to request clarification or add additional context in comments.

Comments

1

If you already get image URL, you can use this lib to load image async from main thread, I just try and it's very easy to use and works fine, hope it helps you: https://github.com/nostra13/Android-Universal-Image-Loader

2 Comments

FYI, if the person is having Image URL then he can use this library otherwise not.
@Paresh Mayani: Of course, if don't have image URL, he can't display image from server.
1

For HTML data:

Based on the code, i can say you are getting HTML data inside images, so you can load this HTML inside the WebView.

For Image URL:

As others suggested, you can use either Android Universal Image Loader or Lazy Loading of Images by Fedor library or any library to load images, only if you are getting Image URL instead HTML data.

Comments

0

Very little of the content is actually JSON. The "key" in this content is named "content" and the value is the rest of it after the first colon ':'. The rest of it is regular HTML (which would need to be pulled out with a regular expression or other HTML parser).

In javascript, the best you can do is:

var text = JSON.parse(content)["content"]; // which gives you everything after the colon.

But then you still need to a regular expression to pull out all the href tags:

Here's an example on the command line:

bash$ content="<p><img class=\"aligncenter\" style=\"cursor: -moz-zoom-in;\" src=\"http:\/\/sphotos-h.ak.fbcdn.net\/hphotos-ak-ash4\/395050_10151219612828815_5123523_n.jpg\" alt=\"http:\/\/sphotos-h.ak.fbcdn.net\/hphotos-ak-ash4\/395050_10151219612828815_5123523_n.jpg\" width=\"390\" height=\"466\" \/><\/p>\n<p><span id=\"more-5267\"><\/span><\/p>\n<p>Some texts here...XXXXXYYYYYZZZZ"

bash$ echo $content | grep -oE "(src|alt)=\"[^\"]+\.jpg\"" | sed "s/\(src=\|alt=\|\"\)//g" | sed "s/\\///g"
http:\\sphotos-h.ak.fbcdn.net\hphotos-ak-ash4\395050_10151219612828815_5123523_n.jpg
http:\\sphotos-h.ak.fbcdn.net\hphotos-ak-ash4\395050_10151219612828815_5123523_n.jpg

You could translate that regexp to whatever language you're using.

3 Comments

Thanks. But I don't know how to do this in Android.
Don't know how to do what, exactly?
@hinesmr probably using grep, sed :-) Try to keep code snippets in relevant technologies/languages as the question requires.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.