gpt4 book ai didi

android - 与 API25 不同的项目中的低速

转载 作者:行者123 更新时间:2023-11-29 01:01:29 25 4
gpt4 key购买 nike

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

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

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

一些建议?谢谢

最佳答案

解决了!

最后我用 Volley 替换了库 loopj 并且神秘地工作得很好。我不知道原因。我想我不会很好地完成这些请求,但有了 Volley,我设法让它适用于所有设备。

谢谢!

关于android - 与 API25 不同的项目中的低速,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50841834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com