android - HTTP Post delayed until online -


Whenever there is a network connection, I successfully submit that code successfully to the database My app is one that is on the road Will be used, often the cell service in places was very poor.

My question is, what will be the best way to delay HTTP post, unless there is an active network connection? Should a service be created that is going to run, if no connection is available, then the connection has been alerted when available? The transmitted data is small, is around one kilobyte, so long as the device is not online, I could get the app store and then could store the data. Does this seem to be?

Another, poor option, the App Store has to be kept near the data and every time it needs to be started if any data is still waiting for the deposit and an active connection to deposit it Is with

I currently have the code to collect the data below. If you have any suggestions about it, then you are welcome too.

  string setpost (reference reference) {HttpClient httpclient = new DefaultHttpClient (); HttpPost httppost = New HttpPost (context.getResources (). GetString (R.string.url)); {List & lt; BasicNameValuePair & gt; NameValuePairs = New ArrayList & lt; BasicNameValuePair & gt; (22); NameValuePairs.add (new binary namewallpayer ("month", "7")); ... etc ... httppost.setEntity (new UrlEncodedFormEntity (nameValuePairs)); HttpResponse response = httpclient.execute (httppost); InputStream = response.getEntity () GetContent (); BufferedInputStream bis = new BufferedInputStream (is); Byterebrum Buff = New Byterebar (20); Int current = 0; While ((current = bis.read ())! = -1) {baf.append (byte) on); } Toast. Make Text (References, "Thank you for submitting!", Toast. LNNHH_LOG). Show (); New string return (baf.toByteArray ()); } Hold (ClientProtocolException E) {LogEE (it.Testuring (), "Failed:" + E); Toast.makeText (Reference, "Submission failed: (\ nPlease report error to the developer below \ n" + e.toString (), Toast.LNGTH_LONG) Show (); Return ("Failed:" + E) ;} Hold (IOException e) {Log.e (this.toString (), "Failed:" + E) Toast. Make Text (Reference, "Submission Failed: (\ nPlease report to the developer the error below: \ N "+ e .toString (), Toast.LENGTH_LONG) Show (); Return (" Failed: "+ E);}    

Should I create a service that has the connection available But there is no connection, so what is going on?

Yes. IntentService to handle all network requests. When no activity < / Code> wants to upload / download anything, then it passes its request to IntentService , the request is queued and ends with IntentService . / P>

Also register a BroadcastReceiver in a main code that 'listens' to network connectivity changes. If the change is for the 'connected' state, then it should start the IntentService , which reprocess requests of a queued network and then terminate itself.

It is efficient and works well in my experience.

Comments