java - Trying to get UTC datetime force closes application -
i designing service class update info server periodically. working fine, have send current utc date time in format "mm/dd/yyyy hh:mm:ss" server. have tried number of ways accomplish this, not giving me desired results.
source code of class:
public class myalarmservice extends service { string device_id; // gpstracker class gpstracker gps; string date_time; @override public void oncreate() { // todo auto-generated method stub //toast.maketext(this, "service created", toast.length_long).show(); //---get record--- } @override public ibinder onbind(intent intent) { // todo auto-generated method stub //toast.maketext(this, "myalarmservice.onbind()", toast.length_long).show(); homecoming null; } @override public void ondestroy() { // todo auto-generated method stub super.ondestroy(); //toast.maketext(this, "service destroyed.", toast.length_long).show(); } @override public void onstart(intent intent, int startid) { // todo auto-generated method stub super.onstart(intent, startid); //toast.maketext(this, "service started.", toast.length_long).show(); //create class object gps = new gpstracker(this); // check if gps enabled if(gps.cangetlocation()) { double latitude = gps.getlatitude(); double longitude = gps.getlongitude(); string locationusing = gps.getlocationusing(); final telephonymanager tm =(telephonymanager)getbasecontext().getsystemservice(context.telephony_service); string deviceid = tm.getdeviceid(); // calendar agmtcalendar = calendar.getinstance(timezone.gettimezone("gmt")); // agmtcalendar.gettimeinmillis(); //or gettimeinmillis() **date d = new date(year); charsequence s = dateformat.format("yyyy-mm-dd hh:mm:ss", d.gettime());** // get_date(); **simpledateformat dateformatgmt = new simpledateformat("yyyy-mm-dd"); dateformatgmt.settimezone(timezone.gettimezone("gmt")); java.util.date date = null; system.out.println(dateformatgmt.format(date)); toast.maketext(getapplicationcontext(),""+date, toast.length_long).show();** //toast.maketext(getapplicationcontext(),"device id: "+deviceid+ " time: "+ s+"\nlat: " + latitude + "\nlong: " + longitude, toast.length_long).show(); log.w( "\nlat: " , ""+latitude); log.w( "\nlong: " , ""+longitude); if(havenetworkconnection()) { // sendpostrequest(deviceid,date_time,""+latitude,""+longitude); // arraylist<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(3); // // namevaluepairs.add(new basicnamevaluepair("lat",""+latitude)); // namevaluepairs.add(new basicnamevaluepair("lng",""+longitude)); // // // seek // { // httpclient httpclient = new defaulthttpclient(); // //httppost httppost = new httppost("http://eatcastle.com/security_system_portal/api/getting_lat_lng.php"); // httppost httppost = new httppost("http://google.com"); // httppost.setentity(new urlencodedformentity(namevaluepairs)); // httpresponse response = httpclient.execute(httppost); // string responsebody = entityutils.tostring(response.getentity()); // // //toast.maketext( getapplicationcontext(),""+responsebody,toast.length_long).show(); // } // grab (throwable t) // { // log.d("error",""+t); // } } else { toast.maketext( getapplicationcontext(),"no net connection or wifi available",toast.length_long).show(); } } else { // gps or network not enabled toast.maketext(getapplicationcontext(), "no network no gps", toast.length_long).show(); } } @override public boolean onunbind(intent intent) { // todo auto-generated method stub // toast.maketext(this, "service binded", toast.length_long).show(); homecoming super.onunbind(intent); } //======================================================================================================= //check packet info , wifi //======================================================================================================= private boolean havenetworkconnection() { boolean haveconnectedwifi = false; boolean haveconnectedmobile = false; connectivitymanager cm = (connectivitymanager) getsystemservice(context.connectivity_service); networkinfo[] netinfo = cm.getallnetworkinfo(); (networkinfo ni : netinfo) { if (ni.gettypename().equalsignorecase("wifi")) if (ni.isconnected()) haveconnectedwifi = true; if (ni.gettypename().equalsignorecase("mobile")) if (ni.isconnected()) haveconnectedmobile = true; } homecoming haveconnectedwifi || haveconnectedmobile; } //======================================================================================================= //checking packet info , wifi end //======================================================================================================= void get_date() { } //sending async post request private void sendpostrequest(string deviceid, string date_time,string latitude, string longitude) { class sendpostreqasynctask extends asynctask<string, void, string>{ @override protected string doinbackground(string... params) { string paramdeviceid = params[0]; string paramdate_time = params[1]; string paramlatitude = params[2]; string paramlongitude = params[3]; //system.out.println("*** doinbackground ** paramusername " + paramusername + " parampassword :" + parampassword); httpclient httpclient = new defaulthttpclient(); // in post request, don't pass values in url. //therefore utilize web page url parameter of httppost argument httppost httppost = new httppost("http://google.com"); // because not passing values on url, should have mechanism pass values can //uniquely separate other end. //to accomplish utilize basicnamevaluepair //things need pass post request basicnamevaluepair deviceidbasicnamevaluepair = new basicnamevaluepair("stringloginuser", paramdeviceid); basicnamevaluepair date_timebasicnamevaluepair = new basicnamevaluepair("stringloginpwd", paramdate_time); basicnamevaluepair latitudebasicnamevaluepair = new basicnamevaluepair("stringloginpwd", paramlatitude); basicnamevaluepair longitudebasicnamevaluepair = new basicnamevaluepair("stringloginpwd", paramlongitude); // add together content want pass post request name-value pairs //now set sending details arraylist type safe of namevaluepair list<namevaluepair> namevaluepairlist = new arraylist<namevaluepair>(); namevaluepairlist.add(deviceidbasicnamevaluepair); namevaluepairlist.add(date_timebasicnamevaluepair); namevaluepairlist.add(latitudebasicnamevaluepair); namevaluepairlist.add(longitudebasicnamevaluepair); seek { // urlencodedformentity entity composed of list of url-encoded pairs. //this typically useful while sending http post request. urlencodedformentity urlencodedformentity = new urlencodedformentity(namevaluepairlist); // setentity() hands entity (here urlencodedformentity) request. httppost.setentity(urlencodedformentity); seek { // httpresponse interface httppost. //therefore can't initialize them httpresponse httpresponse = httpclient.execute(httppost); // according java api, inputstream constructor nothing. //so can't initialize inputstream although not interface inputstream inputstream = httpresponse.getentity().getcontent(); inputstreamreader inputstreamreader = new inputstreamreader(inputstream); bufferedreader bufferedreader = new bufferedreader(inputstreamreader); stringbuilder stringbuilder = new stringbuilder(); string bufferedstrchunk = null; while((bufferedstrchunk = bufferedreader.readline()) != null){ stringbuilder.append(bufferedstrchunk); } homecoming stringbuilder.tostring(); } grab (clientprotocolexception cpe) { system.out.println("first exception caz of httpresponese :" + cpe); cpe.printstacktrace(); } grab (ioexception ioe) { system.out.println("second exception caz of httpresponse :" + ioe); ioe.printstacktrace(); } } grab (unsupportedencodingexception uee) { system.out.println("an exception given because of urlencodedformentity argument :" + uee); uee.printstacktrace(); } homecoming null; } @override protected void onpostexecute(string result) { super.onpostexecute(result); toast.maketext(getapplicationcontext(), result, toast.length_long).show(); } } sendpostreqasynctask sendpostreqasynctask = new sendpostreqasynctask(); sendpostreqasynctask.execute(deviceid, date_time,latitude,longitude); } } how should pick current utc date time in above mentioned format , send server?
p.s.: while implementing different methods, found ide error saying new date() constructor not defined.
why assigning null date value..?
check out:
date formatteddate = new date(system.currenttimemillis()); simpledateformat sdf = new simpledateformat("yyyy mm dd, hh:mm a", locale.us); string time = sdf.format(formatteddate); java android datetime utc
No comments:
Post a Comment