java - Trying to connect an external database (postgreSQL)with my android application -
i'm having issue android application , don't know how resolve it.... i'm trying make connection throw external database (postgresql) , taking informations on there .... made code on java , connection worked (also able queries , receive results)! problem is, if import android application code made on java class, throws me error not understand ....
05-18 16:09:10.273: w/system.err(5395): org.postgresql.util.psqlexception: unusual has occured cause driver fail. please report exception. 05-18 16:09:10.273: w/system.err(5395): @ org.postgresql.driver.connect(driver.java:287) 05-18 16:09:10.273: w/system.err(5395): @ java.sql.drivermanager.getconnection(drivermanager.java:175) 05-18 16:09:10.273: w/system.err(5395): @ java.sql.drivermanager.getconnection(drivermanager.java:209) 05-18 16:09:10.273: w/system.err(5395): @ com.mlab.android.basicoverlays.postgresql.setconnection(postgresql.java:55) 05-18 16:09:10.273: w/system.err(5395): @ com.mlab.android.basicoverlays.postgresql.<init>(postgresql.java:18) 05-18 16:09:10.273: w/system.err(5395): @ com.example.basicmaponline.mainactivity.oncreate(mainactivity.java:31) 05-18 16:09:10.273: w/system.err(5395): @ android.app.activity.performcreate(activity.java:5250) 05-18 16:09:10.273: w/system.err(5395): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1097) 05-18 16:09:10.283: w/system.err(5395): @ android.app.activitythread.performlaunchactivity(activitythread.java:2210) 05-18 16:09:10.283: w/system.err(5395): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2296) 05-18 16:09:10.283: w/system.err(5395): @ android.app.activitythread.access$700(activitythread.java:151) 05-18 16:09:10.283: w/system.err(5395): @ android.app.activitythread$h.handlemessage(activitythread.java:1281) 05-18 16:09:10.283: w/system.err(5395): @ android.os.handler.dispatchmessage(handler.java:99) 05-18 16:09:10.283: w/system.err(5395): @ android.os.looper.loop(looper.java:137) 05-18 16:09:10.283: w/system.err(5395): @ android.app.activitythread.main(activitythread.java:5293) 05-18 16:09:10.283: w/system.err(5395): @ java.lang.reflect.method.invokenative(native method) 05-18 16:09:10.283: w/system.err(5395): @ java.lang.reflect.method.invoke(method.java:511) 05-18 16:09:10.283: w/system.err(5395): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1102) 05-18 16:09:10.283: w/system.err(5395): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:869) 05-18 16:09:10.283: w/system.err(5395): @ dalvik.system.nativestart.main(native method) 05-18 16:09:10.283: w/system.err(5395): caused by: android.os.networkonmainthreadexception 05-18 16:09:10.293: w/system.err(5395): @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1118) 05-18 16:09:10.293: w/system.err(5395): @ java.net.inetaddress.lookuphostbyname(inetaddress.java:385) 05-18 16:09:10.293: w/system.err(5395): @ java.net.inetaddress.getallbynameimpl(inetaddress.java:236) 05-18 16:09:10.293: w/system.err(5395): @ java.net.inetaddress.getbyname(inetaddress.java:289) 05-18 16:09:10.293: w/system.err(5395): @ java.net.inetsocketaddress.<init>(inetsocketaddress.java:105) 05-18 16:09:10.293: w/system.err(5395): @ java.net.inetsocketaddress.<init>(inetsocketaddress.java:90) 05-18 16:09:10.293: w/system.err(5395): @ org.postgresql.core.pgstream.<init>(pgstream.java:60) 05-18 16:09:10.293: w/system.err(5395): @ org.postgresql.core.v3.connectionfactoryimpl.openconnectionimpl(connectionfactoryimpl.java:101) 05-18 16:09:10.293: w/system.err(5395): @ org.postgresql.core.connectionfactory.openconnection(connectionfactory.java:64) 05-18 16:09:10.293: w/system.err(5395): @ org.postgresql.jdbc2.abstractjdbc2connection.<init>(abstractjdbc2connection.java:136) 05-18 16:09:10.293: w/system.err(5395): @ org.postgresql.jdbc3.abstractjdbc3connection.<init>(abstractjdbc3connection.java:29) 05-18 16:09:10.293: w/system.err(5395): @ org.postgresql.jdbc3g.abstractjdbc3gconnection.<init>(abstractjdbc3gconnection.java:21) 05-18 16:09:10.293: w/system.err(5395): @ org.postgresql.jdbc4.abstractjdbc4connection.<init>(abstractjdbc4connection.java:31) 05-18 16:09:10.293: w/system.err(5395): @ org.postgresql.jdbc4.jdbc4connection.<init>(jdbc4connection.java:24) 05-18 16:09:10.293: w/system.err(5395): @ org.postgresql.driver.makeconnection(driver.java:393) 05-18 16:09:10.293: w/system.err(5395): @ org.postgresql.driver.connect(driver.java:267) 05-18 16:09:10.293: w/system.err(5395): ... 19 more
p.s: downloaded throw postgresql website .jar("postgresql-9.2-1002.jdbc4.jar") included on android project ... , java version "java version "1.6.0_45". please. :/
i did tried class activesync seems doesnt work , have no idea ...
//@suppresswarnings("rawtypes") public class loaddatabase extends asynctask<void, void, hashmap>{ @override protected hashmap doinbackground(void... params) { // todo auto-generated method stub hashmap<string,sqlloja>listalojas = new hashmap<string, sqlloja>(); try{ postgresql psql = new postgresql(host,database,username,password); string sql = psql.getlojascidadao(); statement st = psql.getconnection().createstatement(); resultset rs = st.executequery(sql); while(rs.next()){ int lcid = integer.parseint(rs.getstring(1)); string lcnome=rs.getstring(2); string lccp = rs.getstring(3); int lcdistrito = integer.parseint(rs.getstring(4)); int lcconselho = integer.parseint(rs.getstring(5)); double lcaltitude = double.parsedouble(rs.getstring(6)); double lclongitude = double.parsedouble(rs.getstring(7)); string lctelefone = rs.getstring(8); boolean lcestado = boolean.parseboolean(rs.getstring(9)); string lcrua = rs.getstring(10); sqlloja loja = new sqlloja(lcid,lcnome,lccp,lcdistrito,lcconselho,lcaltitude,lclongitude,lctelefone,lcestado,lcrua); listaloja.put(loja.getnome(),loja.clone()); string informacoesloja = "rua : "+lcrua+"\nc.p. : "+lccp+"\ntel. : "+lctelefone; log.d("async",lcnome+" altitude = "+lcaltitude+" longitude = "+lclongitude+" rua = "+lcrua); } //listaloja = new sqllistloja(listalojas); rs.close(); st.close(); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (classnotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (exception e) { e.printstacktrace(); } if(listaloja!=null){ for(sqlloja loja : listaloja.values()){ log.d("async", loja.getnome() ); } } else log.d("async","listaloja = null ! pqp !"); return listalojas; } @override protected void onpostexecute(hashmap listalojas){ listaloja = listalojas; } }
even :
if(listaloja!=null){ for(sqlloja loja : listaloja.values()){ log.d("async", loja.getnome() ); } } else log.d("async","listaloja = null ! pqp !");
it shows me listaloja nullable ... when normally, should not nullable... due dobackground method ( takes informations external database , put them on listaloja ...
:/
/edited post/
intro.java:
public class intro extends activity{ //sqllistloja listaloja; hashmap<string, sqlloja> listaloja; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.intro); //intent openmainactivity = new intent("com.example.basicmaponline.mainactivity"); //isto vem ficheiro androidmanifest.xml o "com.example....." //startactivity(openmainactivity); try { listaloja = new loaddatabase().execute().get(); } catch (interruptedexception e1) { // todo auto-generated catch block e1.printstacktrace(); } catch (executionexception e1) { // todo auto-generated catch block e1.printstacktrace(); }
networkonmainthread exception occurs when running network related operation on main ui thread.
you use should use asynctask purpose
http://developer.android.com/reference/android/os/asynctask.html
check link above topic under heading 4 steps.
example:
class thetask extends asynctask<void,void,void> { protected void onpreexecute() { super.onpreexecute(); //display progressdialog. } protected void doinbackground(void ...params) { //network related opearaiton. not update ui here return null; } protected void onpostexecute(void result) { super.onpostexecute(result); //dismiss progressdialog. //update ui } }
you create own thread. need make sure update ui on ui thread.
Comments
Post a Comment