Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Forums Advisor I
Posts: 501
Registered: ‎03-01-2013
My Device: BlackBerry Z10
Accepted Solution

How to parse image url in json?

Hi

I am receiving response from server and parse it through json.And display it to the field

Like name1 as :

try {
            labelField1.setText(jsonObject2.get("name1").toString());
        }

catch (JSONException e)

{
            System.out.println("JSONException Description");
        }

 

Similarly i want to do for the image. How to parse the image url and display it to the BitmapField??

 

 

Regular Contributor
Posts: 61
Registered: ‎05-22-2011
My Device: Blackberry Torch
My Carrier: Airtel

Re: How to parse image url in json?

[ Edited ]

You need to create a seperate thread to fetch image bytes from the URL you have. 

 

You can use the following code to fetch the image from server. 

public EncodedImage getImageBytesFromURL(String url){
		ConnectionFactory connFactory = null;
		ConnectionDescriptor connDescriptor = null;
		HttpConnection httpConn = null;
		InputStream inputStream = null;
		byte[] response = null;
		try {
			connFactory = new ConnectionFactory();
			connFactory.setConnectionTimeout(10*1000);
			connFactory.setAttemptsLimit(1);
			connDescriptor = connFactory.getConnection(url;interface=wifi);
			if (connDescriptor != null) {
				httpConn = (HttpConnection) connDescriptor.getConnection();
				inputStream = httpConn.openInputStream();
				int responseCode = httpConn.getResponseCode();
				if(responseCode == HttpConnection.HTTP_OK){
					response = readStream(inputStream);
				}
			}
		}catch (IOException e) {
			
		}finally {
			try {
				inputStream.close();
				httpConn.close();
				inputStream = null;
				httpConn = null;
				connDescriptor = null;
				connFactory = null;
			} catch (Exception e) {
				
			}
		}
		EncodedImage encodedImage = null;
		if(response != null){
			encodedImage = EncodedImage.createEncodedImage(response, 0, response.length);
		}
		return encodedImage;
	}

 Remember that this method should be in a background thread. Otherwise your UI will hang. The encodedImage which the above method returns can be easily converted to bitmap and set into your bitmapfield. 

Forums Advisor I
Posts: 501
Registered: ‎03-01-2013
My Device: BlackBerry Z10

Re: How to parse image url in json?

Hi

If i convert the url into bitmap and display it to the bitmap field then everytime i get the same image.

Regular Contributor
Posts: 61
Registered: ‎05-22-2011
My Device: Blackberry Torch
My Carrier: Airtel

Re: How to parse image url in json?

Hi  Vaishali,

 

I didn't understand what you said. I believe that the image URL which is coming in JSON is something like this: http://something.com/hello.png.

If it is like this, then the above code will work to fetch it from server. 

 

Could you please explain what exactly is the issue which you are facing?

Forums Advisor I
Posts: 501
Registered: ‎03-01-2013
My Device: BlackBerry Z10

Re: How to parse image url in json?

See i am doing in this way..

 

        
         Bitmap bitmap= getImageBytesFromURL(url);

          Bitmap finalBitmap = resizeImage(bitmap, imageDimension, imageDimension).getBitmap();
          bitmap.scaleInto(finalBitmap, Bitmap.FILTER_BILINEAR);
          BitmapField bitmapField= new BitmapField();
          BitmapField  bitmapfield = new BitmapField(finalBitmap);
          manager1111.add(bitmapfield);

 

1st time i am getting the correct image and i have saved it. Next time i am getting the new image but the old image is replaced by the new one.So i am getting the 2 new images.

 

Regular Contributor
Posts: 61
Registered: ‎05-22-2011
My Device: Blackberry Torch
My Carrier: Airtel

Re: How to parse image url in json?

I am confused!!

 

In your second post you said you are getting the same image in the bitmap field and now you are saying your old image is replaced by the new one. 

 

If you could tell me what is your requirement or what are you trying to achieve, then I could have get back with a better suggestion. 

Forums Advisor I
Posts: 501
Registered: ‎03-01-2013
My Device: BlackBerry Z10

Re: How to parse image url in json?

See i am getting the image from the server .

Suppose i start my app , currently i am having no image . So i get the image from the server and displayed it to the BitmapField. Next time if i get the image from the server, then the previous image is replaced by the new one.

Like

1st time.. name1= vaishali, image=1

2nd time..name1=vaishali, image=2

                  name2= vanshika, image=2

Regular Contributor
Posts: 61
Registered: ‎05-22-2011
My Device: Blackberry Torch
My Carrier: Airtel

Re: How to parse image url in json?

What you mean by "Next time" here? When you restart the app or take the page again?

 

I wonder why at the second time if you are calling the server for the same image, gives you a second image instead of the old one?

Forums Advisor I
Posts: 501
Registered: ‎03-01-2013
My Device: BlackBerry Z10

Re: How to parse image url in json?

When i take the page again.

Regular Contributor
Posts: 61
Registered: ‎05-22-2011
My Device: Blackberry Torch
My Carrier: Airtel

Re: How to parse image url in json?

If you are hitting the server with the same URL, why would the server give you another image? It should send you back the image right? Can you debug and check whether there is any change in the URL which you are hitting the next time you take the screen?