如何加速Android中的网络服务?

时间:2015-08-15 11:17:17

标签: android web-services

最近我开始学习android。现在我想知道有关Android中的Web服务的信息。

  1. 哪些网络服务在Android中很快?

  2. 我们需要使用任何库来实现Web服务的稳定性吗?

  3. 我的主要目标是减少网络服务加载时间。

    今天我发现,Web服务在API级别下面花费的时间更多,而在API级别上面则更好一些。

    请任何人建议我如何减少网络服务负载。

1 个答案:

答案 0 :(得分:0)

基于JSON的Web服务比XML或任何其他格式轻量级,因此即使在低网络连接中,您也可以获得相当好的性能。

以下是连接基于JSON的Web服务的示例代码

JSONObject jsonObj = new JSONObject();
    jsonObj.put("username", username);
    jsonObj.put("data", dataValue);

    // Create the POST object and add the parameters
    HttpPost httpPost = new HttpPost(url);
    StringEntity entity = new StringEntity(jsonObj.toString(), HTTP.UTF_8);
    entity.setContentType("application/json");
    httpPost.setEntity(entity);

    HttpClient client = new DefaultHttpClient();
    HttpResponse response = client.execute(httpPost);

最好在AsyncTask中进行网络调用,以便在处理Web服务时显示进度条