Thursday, 15 August 2013

android post data to server when slow connection -



android post data to server when slow connection -

my info send server when connection fast when connection weak or slow info not send want send info when connection slow or how can wait till connection fast , post info server below code

class sendmessagen extends asynctask<string, string, string> { string mess="no mess",subjector=""; spanned old_message; int check_no1,success=0; string error = null; public sendmessagen(spanned newmessage, string subjects,int check_no) { // todo auto-generated constructor stub mess = html.tohtml(newmessage); subjector = subjects; old_message = newmessage; check_no1 = check_no; } protected string doinbackground(string... args) { list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("sender",myacc)); params.add(new basicnamevaluepair("reciever",filename)); params.add(new basicnamevaluepair("message",mess+"")); params.add(new basicnamevaluepair("subject",subjector)); params.add(new basicnamevaluepair("id_no",check_no1-1+"")); for(int i=0;i < 10;i++){ httppost request = new httppost(send_text); httpparams httpparams = new basichttpparams(); int some_reasonable_timeout = (int) (30 * dateutils.second_in_millis); httpconnectionparams.setconnectiontimeout(httpparams, some_reasonable_timeout); httpconnectionparams.setsotimeout(httpparams, some_reasonable_timeout); httpclient client = new defaulthttpclient(httpparams); seek { request.setentity(new urlencodedformentity(params)); httpresponse response = client.execute(request); statusline status = response.getstatusline(); if (status.getstatuscode() == httpstatus.sc_ok) { responsehandler<string> responsehandler = new basicresponsehandler(); error = responsehandler.handleresponse(response); success = 1; } else { } } grab (clientprotocolexception e) { // else, if wanted. } grab (ioexception e) { // else, if wanted. } { client.getconnectionmanager().shutdown(); } if(success == 1){ = 10; } if(i == 9 && success == 0){ resend_message(old_message,subjector,check_no1); } } homecoming error; } protected void onpostexecute(string result) { if(result != null){ if (success == 1) { } } } }

please check connection before posting info on server.

firstly check net connection available or not method.

public static boolean isnetworkavailable(context context) { boolean connected = false; connectivitymanager mconnmanager = (connectivitymanager) context.getsystemservice(context.connectivity_service); if (mconnmanager != null) { networkinfo[] netinfo = mconnmanager.getallnetworkinfo(); (networkinfo mnetinfo : netinfo) { if ((mnetinfo.gettypename().equalsignorecase("wifi") || mnetinfo.gettypename().equalsignorecase( "mobile")) && mnetinfo.isconnected()) { log.d("", "========== || connection available || ========="); connected = true; } } } homecoming connected; }

if net connection available check network status , post info server!

public static byte getnetworkstatus(context context) { byte networkstatus = network_not_available; connectivitymanager mconnmanager = (connectivitymanager) context.getsystemservice(context.connectivity_service); if (mconnmanager != null) { networkinfo[] netinfo = mconnmanager.getallnetworkinfo(); (networkinfo mnetinfo : netinfo) { if ((mnetinfo.gettypename().equalsignorecase("wifi") || mnetinfo.gettypename().equalsignorecase( "mobile")) && mnetinfo.isconnected()) { networkstatus = network_available; log.d("", "========== || connection available || ========="); } } } if (networkstatus == network_not_available && (settings.system.getint(context.getcontentresolver(), settings.system.airplane_mode_on, 0) != 0)) { networkstatus = airplane_mode_on; } homecoming networkstatus; }

android

No comments:

Post a Comment