A couple questions/problems about JsonParser, and Duplicate entry detected

62 views
Skip to first unread message

Shay Tessler

unread,
Aug 1, 2014, 10:03:18 AM8/1/14
to codenameone...@googlegroups.com
If you are experiencing an issue please mention the full platform your issue applies to:
IDE: NetBeans/Eclipse/IDEA: eclipse
Desktop OS: windows 7
Simulator : ios 3gs
Device: ----

hey

1) i find jsonParser a bit complicated since i have to CAST values all the time , can u add a JsonMap that extends HashMap and have methods like getAsString(String key), getAsLong(String key), getAsInt(String key), getAsBoolean(String key)
i tried to write something my self , but since "jsonParser.parseJSON" returns an HashMap, i cant CAST it to my map, also trying to overwrite "parseJSON' method is not helping since all variables are private.
i am bit  afraid cause next time you will upgrade jsonParser and you will not return "Double" for long values i will be in trouble.......
how can i use a simple map like this ?
or can i use an external json api like Gson ?

public class JsonMap extends HashMap<String, Object>{
   
   
public ArrayList<JsonMap> getAsArray(String key) {
       
return (ArrayList<JsonMap>)get(key);
   
}
   
   
public String getAsString(String key) {
       
return (String)get(key);
   
}
   
   
public Long getAsLong(String key) {
       
Object val = get(key);
       
if(val != null) {
           
return ((Double)val).longValue();
       
}
       
return null;
   
}
   
   
public Integer getAsInt(String key) {
       
Object val = get(key);
       
if(val != null) {
           
return ((Double)val).intValue();
       
}
       
return null;
   
}
   
   
public Boolean getAsBoolean(String key) {
       
Object val = get(key);
       
if(val != null) {
           
return Boolean.parseBoolean((String)val);
       
}
       
return null;
   
}


}


2) i have a "Form" with a "List" that is populated by network, the network and population is called only once!
when i am scrolling my "list" up and down the console is printing allot of "Duplicate entry detected"
this is my ListCellRenderer

public class MessageListCellTenderer extends Container implements ListCellRenderer<Message> {


   
private Container m_userPhoto = new Container() {
       
protected com.codename1.ui.geom.Dimension calcPreferredSize() {
           
return new Dimension(50, 50);
       
};
   
};    
 
private Label m_from          = new Label();
 
private Label m_message       = new Label();
 
private Object m_photokMaskObj;  
 
 
public MessageListCellTenderer() {
 setLayout
(new BorderLayout());
 
 
Container textContainer = new Container(new BorderLayout());
 textContainer
.addComponent(BorderLayout.NORTH, m_from);
 textContainer
.addComponent(BorderLayout.SOUTH, m_message);
 
 
Image opponentPicMaskImage;
       
try {
            opponentPicMaskImage
= Image.createImage("message_user_mask.png");
            m_photokMaskObj
= opponentPicMaskImage.createMask();
       
} catch (IOException e) {
           
// TODO Auto-generated catch block
            e
.printStackTrace();
       
}
 
        m_userPhoto
.setSize(new Dimension(40, 40));
 
 addComponent
(BorderLayout.WEST , m_userPhoto);
 addComponent
(BorderLayout.CENTER, textContainer);
 
 m_userPhoto
.getStyle().setBackgroundType(Style.BACKGROUND_IMAGE_SCALED_FIT);
 
 
//getStyle().setBorder(Border.)
 
}
 
 
public Component getListCellRendererComponent(List list, Message value, int index, boolean isSelected) {
   
   
String userName = value.getUserFromName();
   
String userImgLink = value.getUserFromPhotoLink();
   
if(ApplicationData.get().getUid() == value.getUserFromUid()) {
        userName
= value.getUserToName();
        userImgLink
= value.getUserToPhotoLink();        
   
}
   
 m_from
.setText(userName);
 m_message
.setText(value.getText());
 
Network.get().downloadUserImageToStorage(userImgLink, new ActionListener() {
           
public void actionPerformed(ActionEvent e) {
               
NetworkEvent evt = (NetworkEvent)e;
               
Image img = (Image)evt.getMetaData();
               
                img
.applyMaskAutoScale(m_photokMaskObj);
                m_userPhoto
.getStyle().setBgImage(img);
                m_userPhoto
.setPreferredSize(new Dimension(50, 50));
           
}
       
});
 
return this;
 
}


 
public Component getListFocusComponent(List list) {
 
return this;
 
}


}


3) how can i make borderBottom to my ListCellRenderer ?

4) the mask is not showing in my ListCellRenderer can u help ?
see attached file, (also making the white transparent not working.....)

thank you 



Shai Almog

unread,
Aug 1, 2014, 10:43:58 AM8/1/14
to codenameone...@googlegroups.com
Hi,
there is another JSON parser in the cn1lib section: http://www.codenameone.com/cn1libs.html
You can also use the processing package which some people find more convenient, I'm not a big fan myself though.

Shay Tessler

unread,
Aug 1, 2014, 11:15:34 AM8/1/14
to codenameone...@googlegroups.com
i also prefer the HasMap idea i think its more easy , but CASTING all the time take the fun from the HashMap idea :)
what about my other questions ?

Shai Almog

unread,
Aug 2, 2014, 1:41:37 AM8/2/14
to codenameone...@googlegroups.com
I think casting gets a bad rap.
The renderer gets called dozens of times for every entry so you should be very careful about making requests from it.  You are effectively slowing down the rendering significantly. Not to mention creating a huge amount of objects during rendering which is really bad.
Add a property to the Message class called downloading and only download  if it is false.

Shay Tessler

unread,
Aug 3, 2014, 2:27:41 AM8/3/14
to codenameone...@googlegroups.com
i am Not making any network request or parsing json inside the Message object, i am parsing the json and creating the Message object before putting them inside the list.

i will use that downloaded flag for my CellRenderer thanks :)

also another issue that i have is that the mask is not showing in my ListCellRenderer can u help ,(also making the white transparent not working.....)
the mask image file is attached in the first post of this thread.

Shai Almog

unread,
Aug 3, 2014, 12:17:55 PM8/3/14
to codenameone...@googlegroups.com
Is this line not a network request? Network.get().downloadUserImageToStorage

Applying a mask doesn't modify the image, it creates a new image which you are ignoring so your code does nothing.

Shay Tessler

unread,
Aug 3, 2014, 1:28:25 PM8/3/14
to codenameone...@googlegroups.com
you are right, i meant not calling to get json over again from the network. to get the Message objects.

this line just warps "createImageToStorage" and handle user images, relative links, and the img cacheId  (also is it recommended to hash the cacheId for shorter string ?).

i was assuming that when i am createImageToStorage , for the second time i will get the image right away cause the image is in storage ?


    public void downloadUserImageToStorage(String photoLink, ActionListener l) {
       
if(photoLink == null) {
           
//TODO call actionlistener with default user image
       
} else {
           
String downloadLink = photoLink;
           
if(photoLink.indexOf("://") == -1) {
                downloadLink
= m_buildDomain + downloadLink;
           
}  
           
ImageDownloadService.createImageToStorage(downloadLink, l, photoLink);
       
}
   
}

Shai Almog

unread,
Aug 4, 2014, 1:38:15 AM8/4/14
to codenameone...@googlegroups.com
Even if it will, this means allocations and reading from storage IO both of which are too slow for a renderer.
A renderer can be invoked multiple times per frame for every entry in the list which will slow your app to a crawl. Both the renderer and the model must be instantaneous and generate no garbage when possible. Otherwise the performance of the entire list will suffer.
Everything must be cached.

Shay Tessler

unread,
Aug 4, 2014, 2:58:12 AM8/4/14
to codenameone...@googlegroups.com
thanks shai,  i will fix my renderer and take that in mind .
i will download Images for all Items in the list, and only then will populate the List.
should i count the Download image, and repaint the list when done ?

Shai Almog

unread,
Aug 4, 2014, 10:45:33 AM8/4/14
to codenameone...@googlegroups.com
Normally for these cases we have the ImageDownloadService and URLImage both of which do most of this seamlessly.
Its OK to repaint the image with every download but you should update the model so download happens only once.
Reply all
Reply to author
Forward
0 new messages