项目中的低速与API25不同

时间:2018-06-13 16:13:02

标签: android build.gradle

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

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

一些建议?感谢

1 个答案:

答案 0 :(得分:1)

解决!

最后我用Volley替换了库loopj并且神秘地完成了它。 我不知道原因。我想我不会很好地处理这些请求,但是对于Volley,我设法让它在所有设备上运行。

谢谢!