Android Offline Request with Volley

前端 未结 3 942
臣服心动
臣服心动 2021-02-07 08:17

I want to give to my user a much better offline user experience, therefore, I want to build a Service which stores all POST, DELETE,

3条回答
  •  甜味超标
    2021-02-07 08:50

    you maybe do a networking validation method :
    1. check the network connection
    YES:
    a) make the volley request
    b) parse the adapter from volley request
    c) and save the json into a private file

    NO:
    a) read the private file
    b) make the adapter from json file downloaded.

    ********************** for example

        if(isconnect){   
        JsonArrayRequest Req = new JsonArrayRequest(url,
                new Response.Listener() {
                    public void onResponse(JSONArray response) {
                      //this the request of your http request 
                       Log.d(TAG, response.toString());
                         // Parsing json
                          for (int i = 0; i < response.length(); i++) {
                            try {
                             ......adapter etc etc
                             }
                            adapter.notifyDataSetChanged();
                            //here write the httprequest (json file) in local memory 
                              try {
                              String textjson= response.toString();
                              fos = context.openFileOutput("request.json",MODE_PRIVATE);
                              fos.write(textjson.getBytes("ISO-8859-1")); //encode
                              fos.close();
                              } catch (IOException e) {
                                 // TODO Auto-generated catch block
                                 e.printStackTrace();
                              }
                        swipeRefreshLayout.setRefreshing(false);  
                     }  
                  }, new Response.ErrorListener() {
                   @Override
                   public void onErrorResponse(VolleyError error) {
                   // VolleyLog.d(TAG, "Error: " + error.getMessage());
                 }   });      AppController.getInstance().addToRequestQueue(movieReq);
                                }
                            else{
                                //noconnection();
                                in the noconnection method u can parse the               listview/recyclerview from json file to make the offline mode } 
    }else{
        //parse the listview/recyclerview from local file. }
    

提交回复
热议问题