应用程序在几次交互后缓慢加载数据

时间:2015-06-29 10:18:11

标签: android mysqli android-asynctask httprequest

我有一个Android应用程序,主要专注于检索显示以及将数据写入在线数据库。我在应用程序中有几个活动,它们使用不同的Asynctasks和http请求来加载不同的数据。当我启动应用程序时,所有内容都会迅速加载,以及当我在不同的活动之间切换时。但是,如果我在它们之间快速切换,最终屏幕变黑,大约一分钟后,应用程序再次出现,数据加载速度非常慢。我不确定这是否是缓存问题或其他问题。

logcat的

06-29 12:23:03.792  14635-14667/com.example.albert.bettertipapp I/System.out﹕ AsyncTask #1 calls detatch()
06-29 12:23:03.792  14635-14667/com.example.albert.bettertipapp W/System.err﹕ org.apache.http.conn.HttpHostConnectException: Connection to http://altesting.byethost33.com refused
06-29 12:23:03.802  14635-14667/com.example.albert.bettertipapp W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:235)
06-29 12:23:03.802  14635-14667/com.example.albert.bettertipapp W/System.err﹕ at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:167)
06-29 12:23:03.802  14635-14667/com.example.albert.bettertipapp W/System.err﹕ at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:125)
06-29 12:23:03.802  14635-14667/com.example.albert.bettertipapp W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.executeSB(DefaultRequestDirector.java:807)
06-29 12:23:03.802  14635-14667/com.example.albert.bettertipapp W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:675)
06-29 12:23:03.802  14635-14667/com.example.albert.bettertipapp W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:570)
06-29 12:23:03.802  14635-14667/com.example.albert.bettertipapp W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:491)
06-29 12:23:03.802  14635-14667/com.example.albert.bettertipapp W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:469)
06-29 12:23:03.802  14635-14667/com.example.albert.bettertipapp W/System.err﹕ at com.example.albert.betterapp.JSONParser.makeHttpRequest(JSONParser.java:70)
06-29 12:23:03.802  14635-14667/com.example.albert.bettertipapp W/System.err﹕ at com.example.albert.betterapp.OldBets$LoadAllGames.doInBackground(OldBets.java:99)
06-29 12:23:03.812  14635-14667/com.example.albert.bettertipapp W/System.err﹕ at com.example.albert.betterapp.OldBets$LoadAllGames.doInBackground(OldBets.java:79)
06-29 12:23:03.812  14635-14667/com.example.albert.bettertipapp W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
06-29 12:23:03.812  14635-14667/com.example.albert.bettertipapp W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-29 12:23:03.812  14635-14667/com.example.albert.bettertipapp W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-29 12:23:03.812  14635-14667/com.example.albert.bettertipapp W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-29 12:23:03.812  14635-14667/com.example.albert.bettertipapp W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-29 12:23:03.812  14635-14667/com.example.albert.bettertipapp W/System.err﹕ at java.lang.Thread.run(Thread.java:841)
06-29 12:23:03.812  14635-14667/com.example.albert.bettertipapp W/System.err﹕ Caused by: java.net.ConnectException: failed to connect to /185.27.134.108 (port 80): connect failed: ETIMEDOUT (Connection timed out)
06-29 12:23:03.822  14635-14667/com.example.albert.bettertipapp W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:114)
06-29 12:23:03.822  14635-14667/com.example.albert.bettertipapp W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
06-29 12:23:03.822  14635-14667/com.example.albert.bettertipapp W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:460)
06-29 12:23:03.822  14635-14667/com.example.albert.bettertipapp W/System.err﹕ at java.net.Socket.connect(Socket.java:833)
06-29 12:23:03.822  14635-14667/com.example.albert.bettertipapp W/System.err﹕ at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
06-29 12:23:03.822  14635-14667/com.example.albert.bettertipapp W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:188)
06-29 12:23:03.822  14635-14667/com.example.albert.bettertipapp W/System.err﹕ ... 16 more
06-29 12:23:03.822  14635-14667/com.example.albert.bettertipapp W/System.err﹕ Caused by: libcore.io.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
06-29 12:23:03.822  14635-14667/com.example.albert.bettertipapp W/System.err﹕ at libcore.io.Posix.connect(Native Method)
06-29 12:23:03.822  14635-14667/com.example.albert.bettertipapp W/System.err﹕ at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
06-29 12:23:03.822  14635-14667/com.example.albert.bettertipapp W/System.err﹕ at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
06-29 12:23:03.822  14635-14667/com.example.albert.bettertipapp W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:112)
06-29 12:23:03.822  14635-14667/com.example.albert.bettertipapp W/System.err﹕ ... 21 more
06-29 12:23:03.822  14635-14667/com.example.albert.bettertipapp E/Buffer Error﹕ Error converting result java.io.IOException: Attempted read on closed stream.

2 个答案:

答案 0 :(得分:0)

您是否在离开活动时取消异步任务,即在onStop()或onPause()上?

答案 1 :(得分:0)

这是一个假设。如果应用程序每次进入活动时都会启动HTTP调用,如果您在它们之间快速切换,则可能会有大量待处理的http请求。您必须尝试以聪明的方式处理http请求,例如在使用Asynctask上的cancel()方法退出活动时停止它们或在HttpRequest上中止。此外,如果可能,您还可以缓存http请求结果。