search

Home  >  Q&A  >  body text

java - Android 弱网络环境下与服务器交互怎么实现???

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

巴扎黑巴扎黑2770 days ago1081

reply all(11)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:32:03

    可以采用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);  

    reply
    0
  • 黄舟

    黄舟2017-04-18 10:32:03

    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?

    reply
    0
  • 怪我咯

    怪我咯2017-04-18 10:32:03

    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.

    reply
    0
  • 黄舟

    黄舟2017-04-18 10:32:03

    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,

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 10:32:03

    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)

    reply
    0
  • 迷茫

    迷茫2017-04-18 10:32:03

    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.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:32:03

    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.

    reply
    0
  • 阿神

    阿神2017-04-18 10:32:03

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

    reply
    0
  • 黄舟

    黄舟2017-04-18 10:32:03

    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

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 10:32:03

    There is something called MQTT

    reply
    0
  • Cancelreply