如何改进我在Android上的Rest通话?

如何改进我在Android上的Rest通话?,android,xml,json,rest,google-play,Android,Xml,Json,Rest,Google Play,我正在为Android制作一个应用程序。我喜欢尽快打剩下的电话。当我以XML的形式获取结果时,需要5秒钟(!)才能获得这样一个简单的XML: <souvenirs> <souvenir> <id>1</id> <name>Example 1</name> <rating>3.4</rating> <photourl>/

我正在为Android制作一个应用程序。我喜欢尽快打剩下的电话。当我以XML的形式获取结果时,需要5秒钟(!)才能获得这样一个简单的XML:

   <souvenirs>
     <souvenir>
       <id>1</id>
       <name>Example 1</name>
       <rating>3.4</rating>
       <photourl>/images/example.jpg</photourl>
       <price>3.50</price>
     </souvenir>
     <souvenir>
       <id>2</id>
       <name>Example 2</name>
       <rating>2.4</rating>
       <photourl>/images/example.jpg</photourl>
       <price>8.50</price>
       </souvenir>
   </souvenirs>
日志显示获取输入流大约需要5000毫秒。。有没有办法加快速度?有人知道安卓市场使用哪种技术吗?这比我的应用程序加载速度快得多


提前感谢!:)

当您试图通过浏览器或其他方式(wget、curl)“手动”获取数据时,需要多长时间

在安卓系统上,你还应该考虑移动网络,这通常比台式电脑慢很多。而且延迟更大


对我来说,这听起来很像后端的问题(例如,试图解析客户端的IP,因此需要很多时间)。

当您试图“手动”获取数据时(通过浏览器或其他方式(wget、curl),需要多长时间

在安卓系统上,你还应该考虑移动网络,这通常比台式电脑慢很多。而且延迟更大


对我来说,这听起来很像后端的问题(例如,试图解决客户端的IP问题,因此需要花费大量时间)。

也许这就是它的实现方式,而您不能什么都不做。这是我的猜测。 我的意见是在你自己的线程(放在后台)和前台(主UI线程)上做所有基于连接的事情

我已经在这方面做了一些尝试,它对我来说已经足够快了。。。这是我的密码:

private static HttpResponse doPost(String url, JSONStringer json) {
    try {
        HttpPost request = new HttpPost(url);
        StringEntity entity;
        entity = new StringEntity(json.toString());

        entity.setContentType("application/json;charset=UTF-8");
        entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json;charset=UTF-8"));
        request.setEntity(entity);

        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpResponse response = httpClient.execute(request);
            return response;
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return null;
}
在其他地方,我称这种方法为:

HttpResponse httpResponse = doPost(url, json);
BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));

它对我来说很好…

也许这就是它的实现方式,而你不能什么都不做。这是我的猜测。 我的意见是在你自己的线程(放在后台)和前台(主UI线程)上做所有基于连接的事情

我已经在这方面做了一些尝试,它对我来说已经足够快了。。。这是我的密码:

private static HttpResponse doPost(String url, JSONStringer json) {
    try {
        HttpPost request = new HttpPost(url);
        StringEntity entity;
        entity = new StringEntity(json.toString());

        entity.setContentType("application/json;charset=UTF-8");
        entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json;charset=UTF-8"));
        request.setEntity(entity);

        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpResponse response = httpClient.execute(request);
            return response;
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return null;
}
在其他地方,我称这种方法为:

HttpResponse httpResponse = doPost(url, json);
BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));

它对我来说很好…

使用Apache HttpClient而不是URLConnection:


编辑(2012-02-07):在较新的android平台上不再适用请阅读:

使用Apache HttpClient而不是URLConnection:



编辑(2012-02-07):在较新的android平台上不再适用请阅读:

您确定服务器不是问题,因为它太慢?您确定服务器不是问题,因为它太慢?无论您使用的是HttpClient或UrlConnection还是信鸽,都不应该花费5秒的时间和一个像样的遥控器。@Heiko Rupp true。。。但是HttpClient似乎比UrlConnectionSo更快,所以我现在尝试了HttpClient。结果:04-20 14:00:47.011:DEBUG/info(7126):json调用持续时间:331204-20 14:01:26.621:DEBUG/info(7126):json调用持续时间:320704-20 14:01:27.631:DEBUG/info(7126):json调用持续时间:100904-20 14:01:42.521:DEBUG/info(7126):json调用持续时间:210304-20 14:01:43.861:DEBUG/info(7126):json调用持续时间:1333 04-20 14:02:00.651:DEBUG/info(7126):json调用持续时间:5148 04-20 14:02:01.841:DEBUG/info(7126):json调用持续时间:1190@Martijn538所以现在通话大约需要2秒?当我打开活动并立即重新启动时(在菜单中来回按大约是2秒。但当我等待半分钟时,大约是3/4秒。无论您使用的是HttpClient或UrlConnection,还是信鸽,都不应该花费5秒的时间来使用一个像样的遥控器。@Heiko Rupp true…但是HttpClient似乎比UrlConnectionSo快,我现在尝试了HttpClient。结果ts:04-20 14:00:47.011:DEBUG/info(7126):json调用持续时间:331204-20 14:01:26.621:DEBUG/info(7126):json调用持续时间:320704-20 14:01:27.631:DEBUG/info(7126):json调用持续时间:100904-20 14:01:42.521:DEBUG/info(7126):json调用持续时间:210304-20 14:01:43.861:DEBUG/info(7126):json调用持续时间:1333 04-20 14:02:00.651:DEBUG/info(7126):json调用持续时间:5148 04-20 14:02:01.841:DEBUG/info(7126):json调用持续时间:1190@Martijn538所以现在调用大约需要2秒。当我打开我的活动并立即重新启动它时(在菜单中来回按大约需要2秒。但当我等待半分钟时,大约需要3/4秒。谢谢,当我在我的计算机上执行url时,只需要大约100毫秒,所以这不是问题。我只是想知道如何使我的应用程序与Android Market一样快。@Martijn在浏览器上尝试一下你的手机。我尝试过,这比在我的应用程序中使用它要快。现在我正在尝试对我的httpClient实现Keepalive。有人知道怎么做吗?我听说在应用程序中重用httpClient可以提高速度。我通过在主活动中创建httpClient来重用它。然后,在启动另一个活动时,我将它传递给我的Intent(putExtra)并使我的HttpClient可打包..这是正确的方法吗?根据我上面描述的过程,调用
doPost
方法需要307毫秒,调用
BufferedReader reader=new BufferedReader需要2毫秒(new InputStreamReader(httpResponse.getEntity().getContent(),“UTF-8”))
我在emulator上尝试过这一点。如果我将emulator的速度和延迟分别设置为628和3毫秒。我突然意识到只有第一次呼叫需要很长时间。现在我实现了我自己的restclient和ZMeda方法。我得到以下结果:呼叫持续时间:2401 json呼叫持续时间:416呼叫持续时间:1485 json call duration:908 call duration:1142 json call duration:864 call duration:1229 json call duration:635 call duration:1459 json call duration:575有什么办法可以加快速度吗?如果你们想试试url:谢谢,当我在我的计算机上执行url时,只需要大约100毫秒,所以这不是问题。我只是我想知道这是怎么回事