java - Android 弱网络环境下与服务器交互怎么实现???
巴扎黑
巴扎黑 2017-04-18 10:30:03
0
11
1037

现在项目上有这么个需求,客户拿着APP去一个深山老林里(或者去一个偏僻的村庄)去和核查情况,网络情况不是很好,有可能是2G网络 也有可能时刻断网,但是他要通过APP与服务器交互了,首先是登录就要与服务器交互,然后紧接着获取一些核查规则来查看,这些都是返回的json字符串,手机端进行解析,但是这种弱网络情况请求应该会很慢很慢吧,有没有一种方式能够变快呢? 我查阅了一些资料,网上说:服务端通过将数据压缩后然后传递给客户端,客户端在进行解压,在解析json并展示···· 哪位大神遇到过这种需求。求给个思路。谢谢!

巴扎黑
巴扎黑

reply all (11)
伊谢尔伦
可以采用Gzip压缩,缩小请求数据的大小,提高网络传输速度。 使用gzip,首先要设置请求消息头Accept-Encoding为gzip。这样,你将会得到一个响应,根据消息头Content-Encoding为gzip你可以知道,传输过来的数据是经过gzip压缩的。另外,消息头Content-Length会告诉你压缩后的数据长度。 客户端 GetMethod method = new GetMethod(url);//生成一个get方法实例 method.setQueryString(queryString);//设置查询字符串 method.addRequestHeader("Accept-Encoding", "gzip");//设置接受响应消息为gzip HttpClient client = new HttpClient();//生成执行get方法的客户端实例 client.executeMethod(method);//执行get方法 InputStream in = method.getResponseBodyAsStream();//获取响应消息体 Header contentEncoding = method.getResponseHeader("Content-Encoding");//获取消息头Content-Encoding判断数据流是否gzip压缩过 if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) { GZIPInputStream gzipIn = new GZIPInputStream(in); int len = Integer.parseInt(method.getResponseHeader("Content-Length").getValue()); byte[] b = new byte[len]; gzipIn.read(b); String json = new String(b); System.out.println(json); } 服务端 byte[] result = data.getBytes("UTF-8"); if(response.getHeader("Accept-Encoding").equalsIgnoreCase("gzip")) { // System.out.println("Before compression, the data size is :"+ result.length); // Using gzip compress the data ByteArrayOutputStream out = new ByteArrayOutputStream(); GZIPOutputStream gout = new GZIPOutputStream(out); gout.write(json.getBytes("UTF-8")); gout.close(); result = out.toByteArray(); // System.out.println("After compression, the data size is "+gzipResult.length); this.getResp().setHeader("Content-Encoding","gzip"); this.getResp().setHeader("Content-Length", result.length+""); } response.getOutputStream().write(result);
    黄舟

    Data compression is used to reduce the amount of data transmission. It can increase the speed under weak network, but it will be slow when there is no network. Is it possible to make a prediction model and predict the pre-sending of request content in the next time period based on the relevant parameters provided by the app?

      刘奇

      Cache the last request. The server will only deliver the changed content under weak network conditions, and insert the changed content into the last cache for display. This network is the most economical.

        黄舟

        Data caching, the requested data can be cached locally for data display, and the data that needs to be uploaded can also be cached locally first and transmitted when the network signal is good. Wait for failed reconnection,

          小葫芦

          Compression is needed to improve the efficiency of network transmission and shorten the request interaction time as much as possible.
          Also pay attention to prevent duplicate submissions. (Guarantee the idempotence of some interfaces)

            迷茫

            Weak network means there is a network. As long as you ensure that the network is constantly connected before the server responds, it will be easy to solve. Make sure that the size of the request body and return body is small enough to have no friends. Compress it as much as possible...after receiving the response. Just parse it on the client side.

              Ty80

              Data caching is needed to ensure that the previous data is displayed on the page before new data is obtained; data that has been read is cached to avoid repeated network requests; data compression is mentioned above.

                阿神

                Use xml to transfer data, save it locally, and display local data when there is no network connection

                  黄舟

                  SQLite stores some data. Sensitive data is abbreviated as parameter length. It is judged whether there is a return value in http and then sent again

                    小葫芦

                    There is something called MQTT

                      Latest Downloads
                      More>
                      Web Effects
                      Website Source Code
                      Website Materials
                      Front End Template
                      About us Disclaimer Sitemap
                      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!