简体   繁体   English

Android-缓慢启动新活动

[英]android - slow starting new Activity

Second Activity in my project starts very slow, and I don't know how to fix it. 我的项目中的“第二个活动”的启动速度很慢,我不知道如何解决。 How it works (everything's in onCreate()): firstly it does get-request to the page with json: 它是如何工作的(一切都在onCreate()中):首先,它使用json来获取对页面的请求:

try {
        DefaultHttpClient hc = new DefaultHttpClient();
        ResponseHandler<String> res = new BasicResponseHandler();
        HttpGet getMethod = new HttpGet(url);
        String response = hc.execute(getMethod, res);
        resStr = response.toString();
    } catch (Exception e) {
        System.out.println("Exp=" + e);
    }

I know some methods from here are deprecated, but I don't know how to make it more optimized. 我知道这里不推荐使用某些方法,但是我不知道如何使其更优化。 As a result it returns a string with JSON array of about 32 Objects. 结果,它返回带有约32个对象的JSON数组的字符串。 Then I fill 6 arrays to pass them to ArrayAdapter for filling ListView. 然后,我填充6个数组以将它们传递给ArrayAdapter来填充ListView。 Methods of getting different types of data looks like this: 获取不同类型数据的方法如下所示:

public static String GetWantedType(String resStr, int num) {
    String jsonvalues = "";
    try {
        JSONArray json_Array = new JSONArray(resStr);
        JSONObject json_data = json_Array.getJSONObject(num);
        jsonvalues = json_data.getString("wanted_type");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return jsonvalues;
}

Maybe I should have created json_Array one time outside filling arrays and just pass it to those methods as a JSONArray, not as a string - I don't know if it influence the speed. 也许我应该在填充数组之外创建一次json_Array,然后将其作为JSONArray而不是字符串传递给这些方法-我不知道它是否会影响速度。 In the ArrayAdapter everything seems to be all right, I'm using ViewHolder and my scrolling is excelent. 在ArrayAdapter中,一切似乎都很好,我使用的是ViewHolder,并且滚动效果非常好。 Unlike starting Activity. 不像开始活动。

How to make it better and more optimized? 如何使其更好,更优化?

First, don't do all operations in onCreate() , prefer to do in onResume() 首先,不要在onCreate()进行所有操作,而最好在onResume()

Second, all server call should be in background thread using Async and use result for displaying user. 其次,所有服务器调用都应使用Async在后台线程中,并使用结果显示用户。

If you don't want to call multiple times API for data during onResume() and onPause() , you can consume result of data in array or something and when onResume() call, you can check whether it has data, just load it, else fetch from server. 如果您不想在onResume()onPause()期间多次调用数据API,则可以使用数组中数据的结果,也可以在onResume()调用时检查是否有数据,只需加载,否则从服务器获取。

As Gaurav said the problem is that the network request is called on the main thread. 正如Gaurav所说,问题在于在主线程上调用了网络请求。 At the time you ask a network call your program say : OK STOP I WAIT THE RESPONSE. 在您要求网络调用程序时,请说:OK STOP I WAIT RESPONSE。 So if you want to change this you can do various things. 因此,如果您要更改此设置,则可以执行各种操作。 For example you can use a Asynchronous Network call with a lib (loopj lib) or you can simply open a Thread : do the network call. 例如,您可以使用带有lib(loopj lib)的异步网络调用,也可以简单地打开Thread:进行网络调用。

With that your UI will not freeze 这样,您的用户界面将不会冻结

Are you doing the network call on the main thread? 您是否正在主线程上进行网络呼叫? Please don't do it. 请不要这样做。 Instead do the network operations on different thread. 而是在不同的线程上执行网络操作。

For networking you can use a library retrofit . 对于联网,您可以使用库改造 It also makes it easy for you do the operations asynchronously, by using callbacks or using Observables from RxJava 通过使用回调或RxJava中的Observable,它还使您可以轻松地异步执行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM