I am working on an app where I need to POST data(a JSON string) to a web server. I am doing this using the Apache HTTP Client(attached code below). When there is an internet connection everything works fine, but the project spec needs me to cache data locally if there is no internet connection and wait till I have a connection to the server.

Here are my questions: 1. What would be the best way to cache the JSON strings locally and send them on their way when I get an internet connection. The only way I can think of is to store them in a database till I find a connection to the server.

  1. How do I check if I have an Internet connection?

The code from the POST is here, If I have to catch the data I am assuming I will have to do it in place of the "no connection" error dialogue in the exception handle.

Thread t = new Thread() 
     {  

         public void run() 
            {
                Looper.prepare(); //For Preparing Message Pool for the child Thread
                HttpClient client = new DefaultHttpClient();
                HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
                HttpResponse response;
                try {
                        HttpPost post = new HttpPost("http:xxxxx");

                        StringEntity se = new StringEntity( flat.toString());  
                        se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                        post.setEntity(se);
                        response = client.execute(post);


                    //  /*Checking response */
                        if(response!=null){
                            InputStream in = response.getEntity().getContent(); //Get the data in the entity
                        }

                    } catch(Exception e) 
                    {
                            e.printStackTrace();
                            //createDialog("Error", "Cannot Estabilish Connection");
                            Toast.makeText(getApplicationContext(), "cannot establish concetion",Toast.LENGTH_SHORT).show();
                    }

                Looper.loop(); //Loop in the message queue
            }
        };

        t.start();     
有帮助吗?

解决方案

  1. What would be the best way to cache the JASON strings locally and send them on their way when I get an internet connection.

You can put them to DB, but I would actually store them in local app file, path to this file can be stored in SharedPreferences.

How do I check if I have an Internet connection?

Use android.net.conn.CONNECTIVITY_CHANGE broadcast: Broadcast receiver for checking internet connection in android app

inside this broadcast receiver, you can check Your DB or local file whether there is something to send to server.

Be aware that connectivity can be back even when your app was put to background, and this is when android might kill your app. Broadcast (if put in manifest) will resurect your app and allow to resend any json data.

其他提示

For checking if there's internet connection use this: https://stackoverflow.com/a/4239019/2343384

And just make the sending in another thread, which loops after 5-10 seconds and checks if there's internet connection. And if it's true then sends the JSON string.

And JSON string you can save locally in SharredPreferences as shown here: https://stackoverflow.com/a/22048041/2343384

I'm trying share the easy solution for this:

  • Easily serialize and cache your objects to disk using key/value pairs Reservoir lib
  • Try to use this awesome APJQ lib which handles all your requirement in one shot, click here for my simplified example.
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top