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
Contributor
Posts: 35
Registered: ‎02-02-2012
My Device: 9300
My Carrier: Vodafone
Accepted Solution

App runs slowly with JSON

Hi,

I've got this code:

 

publicvoidgetUpdateDenda()

{

Denda denda;

 

String url=url_lehen_atala+"/getDendak";

 

//Fist I do a connection to a Web Server.

String resultado=getHttpConnection(url);

//If result is not null, then I want to treat that information.

if(resultado!=null)

{

 

JSONArray json =ull;

 

int i=0;

 

try

{

//The web service returns a JSON array, wich has got JSON Objects. So fistly I turn to json Array the string I got form the http conection.

json =newJSONArray(resultado);

 

int l=json.getLenght();

JSONObject jObj=newJSONObject();

 

for(i=0; i<=l;i++)

{

jObj=json.getJSONObject(i);

int id=jObj.getInt("id");

String izena=jObj.getString("izena");

String helbidea=jObj.getString("helbidea");

String telefonoa=jObj.getString("telefonoa");

String argazkia=jObj.getString("argazkia");

 

int mota=jObj.getInt("mota");

String produktuak=jObj.getString("produktuak");

String ordutegia=jObj.getString("ordutegia");

String horario= jObj.getString("horario"); 

int latitud=jObj.getInt("latitud");

int longitud=jObj.getInt("longitud");

String web=jObj.getString("web");

 

int txartela=jObj.getInt("txartela");

String berezitasunak= jObj.getString("berezitasuna");

String emaila=jObj.getString("emaila");

String especialidades=jObj.getString("especialidad");

 

//I do an object with the information I've recived

denda=newDenda(id,izena,helbidea,telefonoa,emaila,mota,produktuak,ordutegia,horario,latitud,longitud,argazkia,web,txartela,berezitasunak,especialidades);

i++;

//I call to a function where I put the object to the local database (SQLite)

sq.dendakSartu(denda);

 

}

 

}

catch(Exception e)

{

e.printStackTrace();

 

}

 

}

 

}

 

The code works, but now I'm trying to receive a JSONArray which has got 200 objects, and this code goes very slowly.

 

Any idea of how to do this to make the app go faster?

 

Thank you!

Developer
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: App runs slowly with JSON

Here are couple of things you can try:

 

1. Remove the Declarations from the loop. Instead of:

for(...)

{

  String a = new ("a");

}

 

Use this:

 

String a= null;

for(...)

{

  a = new ("a");

}

 

2. Instead of calling the DB in each iteration, consider calling it once after the loop (bulk operation).

 

Hope that helps,

 

E.

Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: App runs slowly with JSON

do you run this on a separate thread? if not, do so.
don't commit stuff to the db within the iteration, better to commit it all at once.

as far as style is concerned: i would not use a bean with that many arguments, better create it empty and use setters to fill it, much less prone to errors this way.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
Posts: 35
Registered: ‎02-02-2012
My Device: 9300
My Carrier: Vodafone

Re: App runs slowly with JSON

Hi,

Thank you both!

Simon, you ask me if I do this in a separate thread. My first thought was to do  that, but as SQLite databases can only have a connection each time (if I'm wrong please correct me), and my next screen makes a query to the database I can't run it in another thread.

(The code I've pasted its a part of my updater, I need to update my database before I go trought my next screen).

About  commiting al the stuff in each interaction, maybe I'm quite lost qith databases, but, how can I commit the stuff all together.. Do I have to do an objectArray, and then send it to the SQL part?

Nowadays my SQL code looks like this:

 

Statement st;

st =db.createStatement("INSERT INTO Dendak(ID,Izena,Helbidea,Telefonoa,Emaila,Mota,Produktuak,Ordutegia,Horario,Latitud,Longitud,Argazkia,Web,Txartela,Berezitasunak,Especialidades) " +"VALUES ('"+denda.getId()+"','"+denda.getIzena()+"', '"+denda.getHelbidea()+"', '"+denda.getTelefonoa()+"','"+denda.getEmaila()+"','"+denda.getMota()+"','"+denda.getProduktuak()+"','"+denda.getOrdutegia()+ "','"+denda.getHorario()+"','"+denda.getLatitud()+"','"+denda.getLongitud()+"','"+denda.getArgazkia()+"','"+denda.getWeb()+"','"+denda.getTxartela()+"','"+denda.getBerezitasunak()+"','"+denda.getEspecialidades()+"')");

st.prepare();

st.execute();

st.close();

 

Thank you very much!

 

 

Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: App runs slowly with JSON

parsing, database access and the like are all possibly blocking operations and should not be executed on the event thread.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
Posts: 35
Registered: ‎02-02-2012
My Device: 9300
My Carrier: Vodafone

Re: App runs slowly with JSON

I've done it, I run the complete updater in a thread.

But now I want to avoid the part where I'm putting the stuff into the database.

I'me triying somethin like:

 

INSERT INTO Database (Id, Name) VALUES (1,'Mary'),(2,'Paul')

 

But it doesn't work.

Any idea of what's happening?

Any better way to do it?

Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: App runs slowly with JSON

i don't work with databases a lot, but i think opening and closing the connection is the part with the most load.
maybe it is better to parse the json first and then write everything into the database without opening and closing the connection in each iteration loop - if that is feasible.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: App runs slowly with JSON

> But it doesn't work.

What doesn't work about it?

 

> Any idea of what's happening?

Good question, what IS happening? Are you getting an exception or other error?

Contributor
Posts: 35
Registered: ‎02-02-2012
My Device: 9300
My Carrier: Vodafone

Re: App runs slowly with JSON

Thanks for your answer.

 

I'm trying this code, and it doesn't work:

 

st =db.createStatement("INSERT INTO People (ID,name) VALUES (1,'Mary'), (2,'Paul')");

st.prepare();

st.execute();

st.close();

 

I don't get any exception, but database is empty.

 

Any idea?

 

 

Developer
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: App runs slowly with JSON

This syntax is not supported on sqlite.

 

Use statement.bind(...) and then statement.execute() or statement.executeInsert() to insert the data.

 

Check out this link:

http://docs.blackberry.com/en/developers/deliverables/29299/Creating_and_deleting_tables_1596871_11....

 

E.