首页 > 解决方案 > 与 API25 不同的项目中的低速

问题描述

我在 API 不同于 25 的设备上遇到了低速问题,我不知道如何解决。我目前正在一个应用程序中工作,该应用程序连接到远程数据库并从 Web 服务读取数据,然后将其显示在屏幕上。这是应用程序主菜单的一个简单示例:

public class PrincipalActivity extends AppCompatActivity {

ImageView img_citas;
String cod_persona;
String nom;
String fecha_actual=String.valueOf(android.text.format.DateFormat.format("dd-MM-yyyy", new java.util.Date()));


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_principal);


    img_citas = (ImageView)findViewById(R.id.img_citas);

    cod_persona = 9;
    nom = "fisios";

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

    setSupportActionBar(toolbar);

    ObtDatos();

}

public void ObtDatos(){

    AsyncHttpClient client = new AsyncHttpClient();
    String url = "http://192.168.0.252:62/datos/policlinica/webservices/nombre_fisio.php?COD_PERSONA="+cod_persona;


    client.post(url, new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

            if (statusCode==200){ 
                nom = obtCodigo(new String(responseBody)); 

                if (getSupportActionBar() != null) {     

                    if (!nom.equals("")) {  
                        getSupportActionBar().setTitle(nom);
                        getSupportActionBar().setSubtitle(fecha_actual);

                    } else {
                        getSupportActionBar().setTitle("Policlínica");
                    }
                }

                img_citas.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Intent i = new Intent(PrincipalActivity.this, HorariosActivity.class);
                        startActivity(i);
                    }
                });
            }
        }


        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

        }
    });

}

public String obtCodigo(String response){ 

    String cod_usu="";

    try{
        JSONArray json = new JSONArray(response);

        if (json.length()>0){   

            cod_usu = json.getJSONObject(0).getString("NOMBRE");
             }else{

            cod_usu="";
        }


    }catch (Exception e){
        e.getMessage();
    }

    return cod_usu;
}
}

基本上,它读取远程数据库中的人名并将其放置在工具栏中。不应该超过 2 秒的事情,但我需要 9 秒!

以防万一,我还附上了gradle文件的内容,以防出现问题:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "27.0.3"
    defaultConfig {
        applicationId "com.example.cristobal.policlinica"
        minSdkVersion 23
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.loopj.android:android-async-http:1.4.9'
    testCompile 'junit:junit:4.12'
}

该应用程序运行良好,但仅适用于具有 API 25 (Android 7) 的移动设备。其余的,例如 API 23 (Android 6) 或 API 28 (Android 8),虽然它可以很好地从数据库中读取并显示它们,但它的速度非常低。前面的代码非常简单,几乎需要 9 秒才能完整阅读并显示在屏幕上。虽然使用 API 25,但它实际上是即时的。

我敢肯定,有些东西让我无法理解,但我认为我太新手了,看不到问题所在。

关于发生在我身上的任何建议?

编辑:

这是一切正常的设备的 logCat:

06-14 09:43:25.837 6965-7103/com.example.cristobal.policlinica D/NetworkSecurityConfig: No Network Security Config specified, using platform default
06-14 09:43:26.333 6965-6976/com.example.cristobal.policlinica I/art: Background sticky concurrent mark sweep GC freed 4305(281KB) AllocSpace objects, 1(20KB) LOS objects, 0% free, 24MB/24MB, paused 7.051ms total 50.743ms
06-14 09:43:26.462 6965-6976/com.example.cristobal.policlinica I/art: Background partial concurrent mark sweep GC freed 568(30KB) AllocSpace objects, 0(0B) LOS objects, 13% free, 24MB/28MB, paused 5.509ms total 117.376ms
06-14 09:43:27.303 6965-6972/com.example.cristobal.policlinica W/art: Suspending all threads took: 6.837ms
06-14 09:43:27.654 6965-6965/com.example.cristobal.policlinica V/AsyncHttpRH: Progress 72 from 72 (100%)

而这个是一个需要很长时间的设备的logcat:

06-14 10:04:24.916 3086-3347/com.example.cristobal.policlinica D/NetworkSecurityConfig: No Network Security Config specified, using platform default
06-14 10:04:25.973 3086-3097/com.example.cristobal.policlinica W/art: Suspending all threads took: 95.884ms
06-14 10:04:26.009 3086-3093/com.example.cristobal.policlinica W/art: Suspending all threads took: 20.145ms
06-14 10:04:26.016 3086-3364/com.example.cristobal.policlinica W/art: Verification of java.lang.String cz.msebera.android.httpclient.protocol.DefaultedHttpContext.toString() took 143.593ms
06-14 10:04:26.016 3086-3097/com.example.cristobal.policlinica I/art: Background partial concurrent mark sweep GC freed 5085(329KB) AllocSpace objects, 1(20KB) LOS objects, 29% free, 9MB/13MB, paused 106.940ms total 471.003ms
06-14 10:04:26.414 3086-3093/com.example.cristobal.policlinica W/art: Suspending all threads took: 11.969ms
06-14 10:04:31.861 3086-3086/com.example.cristobal.policlinica V/AsyncHttpRH: Progress 72 from 72 (100%)

这3行...

06-14 10:04:25.973 3086-3097/com.example.cristobal.policlinica W/art: Suspending all threads took: 95.884ms
06-14 10:04:26.009 3086-3093/com.example.cristobal.policlinica W/art: Suspending all threads took: 20.145ms
06-14 10:04:26.016 3086-3364/com.example.cristobal.policlinica W/art: Verification of java.lang.String cz.msebera.android.httpclient.protocol.DefaultedHttpContext.toString() took 143.593ms 

...不同,我认为它们导致了慢速模式,但我不知道如何解决它。

一些忠告?谢谢

标签: androidbuild.gradle

解决方案


解决了!

最后我用 Volley 替换了库 loopj 并且神秘地它完美地工作了。我不知道原因。我想我不会很好地完成这些请求,但使用 Volley,我设法让它在所有设备上都能正常工作。

谢谢!


推荐阅读