Android App does not open alert dialog -
i'm making android application requires internet connection. when i'm connected internet, app works fine. want if i'm not connected internet, must display alert dialog notifying user needs internet connection. i'm using async task send data server. code async task :
protected class sendcontacts extends asynctask<string,void,string> { @override protected string doinbackground(string... str) { string contactinfo; contactinfo=str[0]; inputstream = null; string result=new string(); httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://johnconnor.comuf.com/myphp.php"); try { list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(1); namevaluepairs.add(new basicnamevaluepair("fullinfo", contactinfo)); // namevaluepairs.add(new basicnamevaluepair("lat",latitude)); // namevaluepairs.add(new basicnamevaluepair("lng",longitude)); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpresponse response= httpclient.execute(httppost); log.i("sendcontacts successful",response.getstatusline().tostring()); httpentity entity = response.getentity(); = entity.getcontent(); } catch (clientprotocolexception e) { log.e("log_tag", "error in http connection "+e.tostring()); } catch (ioexception e) { // when not connected internet, reach catch block.how can implement alert dialog here can displayed on main ui thread log.e("log_tag","error in http connection:"+e.tostring()); } try { bufferedreader reader = new bufferedreader(new inputstreamreader(is, "iso-8859-1"), 8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); result = sb.tostring(); } catch (exception e) { } result=result.substring(0,result.indexof('\n')); return result ; } }
here logcat:
08-19 14:48:15.130: d/activitythread(10279): settargetheaputilization:0.25 08-19 14:48:15.130: d/activitythread(10279): settargetheapidealfree:8388608 08-19 14:48:15.130: d/activitythread(10279): settargetheapconcurrentstart:2097152 08-19 14:48:15.220: d/abslistview(10279): motionrecognitionmanager 08-19 14:48:15.490: e/log_tag(10279): error in http connection:java.net.unknownhostexception: unable resolve host "johnconnor.comuf.com": no address associated hostname 08-19 14:48:15.500: w/dalvikvm(10279): threadid=12: thread exiting uncaught exception (group=0x41c66438) 08-19 14:48:15.510: w/dalvikvm(10279): threadid=11: thread exiting uncaught exception (group=0x41c66438) 08-19 14:48:15.510: e/androidruntime(10279): fatal exception: asynctask #2 08-19 14:48:15.510: e/androidruntime(10279): java.lang.runtimeexception: error occured while executing doinbackground() 08-19 14:48:15.510: e/androidruntime(10279): @ android.os.asynctask$3.done(asynctask.java:299) 08-19 14:48:15.510: e/androidruntime(10279): @ java.util.concurrent.futuretask$sync.innersetexception(futuretask.java:273) 08-19 14:48:15.510: e/androidruntime(10279): @ java.util.concurrent.futuretask.setexception(futuretask.java:124) 08-19 14:48:15.510: e/androidruntime(10279): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:307) 08-19 14:48:15.510: e/androidruntime(10279): @ java.util.concurrent.futuretask.run(futuretask.java:137) 08-19 14:48:15.510: e/androidruntime(10279): @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230) 08-19 14:48:15.510: e/androidruntime(10279): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1076) 08-19 14:48:15.510: e/androidruntime(10279): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:569) 08-19 14:48:15.510: e/androidruntime(10279): @ java.lang.thread.run(thread.java:856) 08-19 14:48:15.510: e/androidruntime(10279): caused by: java.lang.stringindexoutofboundsexception: length=0; regionstart=0; regionlength=-1 08-19 14:48:15.510: e/androidruntime(10279): @ java.lang.string.startendandlength(string.java:593) 08-19 14:48:15.510: e/androidruntime(10279): @ java.lang.string.substring(string.java:1474) 08-19 14:48:15.510: e/androidruntime(10279): @ com.example.findmyfriend.databaseconnection$registerme.doinbackground(databaseconnection.java:121) 08-19 14:48:15.510: e/androidruntime(10279): @ com.example.findmyfriend.databaseconnection$registerme.doinbackground(databaseconnection.java:1) 08-19 14:48:15.510: e/androidruntime(10279): @ android.os.asynctask$2.call(asynctask.java:287) 08-19 14:48:15.510: e/androidruntime(10279): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:305) 08-19 14:48:15.510: e/androidruntime(10279): ... 5 more
you override onprogressupdate
asynctask
, runs on main ui thread. doinbackground
call publishprogress
trigger it.
protected class sendcontacts extends asynctask<string,void,string> { alertdialog dialog; public sendcontacts() { //configure alertdialog dialog = ... } @override protected string doinbackground(string... str) { string contactinfo; contactinfo=str[0]; inputstream = null; string result=new string(); httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://johnconnor.comuf.com/myphp.php"); try { list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(1); namevaluepairs.add(new basicnamevaluepair("fullinfo", contactinfo)); // namevaluepairs.add(new basicnamevaluepair("lat",latitude)); // namevaluepairs.add(new basicnamevaluepair("lng",longitude)); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpresponse response= httpclient.execute(httppost); log.i("sendcontacts successful",response.getstatusline().tostring()); httpentity entity = response.getentity(); = entity.getcontent(); } catch (clientprotocolexception e) { log.e("log_tag", "error in http connection "+e.tostring()); } catch (ioexception e) { // when not connected internet, reach catch block.how can implement alert dialog here can displayed on main ui thread //update progress publishprogress(null); log.e("log_tag","error in http connection:"+e.tostring()); } try { bufferedreader reader = new bufferedreader(new inputstreamreader(is, "iso-8859-1"), 8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); result = sb.tostring(); } catch (exception e) { } result=result.substring(0,result.indexof('\n')); return result ; } @override //invoked on ui thread protected void onprogressupdate(void... progress) { dialog.show(); } }
Comments
Post a Comment