开发者

How to improve my Rest Calls on Android?

开发者 https://www.devze.com 2023-02-27 14:56 出处:网络
I am making an app for Android. I like to make the rest calls as quick as possible. When I get my results as XML it takes 5 seconds (!) to get a simple xml like this:

I am making an app for Android. I like to make the rest calls as quick as possible. When I get my results as XML it takes 5 seconds (!) to get a simple xml like this:

   <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>

So I tried it with JSON. But that takes also about 5 seconds to retrieve.

I load the XML in android with the following code:

        URL url = new URL("http://example.nu?method=getAllSouvenirs");
            URLConnection conn = url.openConnection();
            long t=System.currentTimeMillis();

            InputStream ins = conn.getInputStream();
            Log.d("info", String.valueOf((System.currentTimeMillis()-t)));

The log says it takes about 5000 ms to get the inputstream.. Is there any way to speed this up? does anybody knows which technique the Android Market uses? This loads way faster than my app..

Thanks in advance! :)


When you try to get the data "manually" - via browser or via other means (wget, curl) how long does it take there.

On Android you also should take the mobile network into consideration that is usually significantly slower than for a desktop computer. Also latencies are bigger.

To me this sounds a lot like issues in the backend (e.g. trying to resolve the IP of the client and thus taking lots of time).


use Apache HttpClient instead of URLConnection: Apache http client or URLConnection

EDIT(2012-02-07): no longer true on newer android platform please read: http://android-developers.blogspot.com/2011/09/androids-http-clients.html


Maybe that is how it is implemented and you can't do nothing. That is my guess. My opinion is to do all connection based stuff on your own thread (to put in in background) and in foreground (main UI thread) entertain user. :)

I have played a little bit around this and it works fast enough for me... Here is my code:

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;
}

And somewhere else I call that method like:

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

It works fine for me...

0

精彩评论

暂无评论...
验证码 换一张
取 消