gpt4 book ai didi

java - Android AsyncTask doInBackground 适用于 1 部手机,但不适用于其他手机

转载 作者:行者123 更新时间:2023-11-30 10:17:37 34 4
gpt4 key购买 nike

我有一个应用程序在我的 Samsung Galaxy S7 7.0 版 API 24 中运行正常。但是当我在华为 CAM-L03 6.0 版 API 23 中尝试它时它不起作用。

基本上我有这样一个类:

public class Sender extends AsyncTask {...}

具有onPreExecutedoInBackgroundonPostExecute 方法,用于使用互联网与php 文件进行通信。当我在 Samsung Galaxy S7 上运行它并执行 Toast.makeText 时,以每种方法在屏幕上显示文本时,它工作得非常好。但是当我在华为 CAM-L03 上运行它时,只显示来自 onPreExecuteonPostExecute 的文本,但从未进入 doInBackground,我不知道为什么。

调用类我这样做:

Sender s = new Sender(MainActivity.this, URL_Total);

s.execute();


不知道是版本问题,还是SDK问题,还是其他问题。感谢您的帮助。

这是我的build.gradle:

apply plugin: 'com.android.application'

android {
compileSdkVersion 26
defaultConfig {
applicationId "com.example.pruebainsp"
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation 'com.android.support:design:26.1.0'
implementation files('libs/PhotoUtil.jar')
implementation files('libs/GenAsync.1.2.jar')
}


编辑 1: toast 消息并不是真正的问题..我只是为 View 添加它们,只是为了检查方法。真正的麻烦是 DoInBackground 不运行或做任何事情。

这是 Sender 类的代码。对于三星 Galaxy S7,它会进入 if (MainActivity.ResponseWeb != null),但在华为 CAM-L03 中,ResponseWeb 始终为空,因此每次我收到消息 Toast。 makeText(c, "Error: response "+ MainActivity.ResponseWeb , Toast.LENGTH_SHORT).show();,永远不要为连接做private String send(),因为doInBackground 在这里不起作用(仅在华为手机中)。

GO TO EDIT 2


编辑 2:我用日志更新了代码,所以这里是:

public class Sender extends AsyncTask<Void,Void,String> {
Context c;
String urlAddress;
ProgressDialog pd;
String fileTodosUsers = "TodosUsuarios";

private static final String TAG = Sender.class.getSimpleName();
/*
1.OUR CONSTRUCTOR
2.RECEIVE CONTEXT,URL ADDRESS AND EDITTEXTS FROM OUR MAINACTIVITY
*/
public Sender(Context c, String urlAddress) {
this.c = c;
this.urlAddress = urlAddress;
//Log.v(TAG, "Mensaje 2");
}
/*
1.SHOW PROGRESS DIALOG WHILE DOWNLOADING DATA
*/
@Override
protected void onPreExecute() {
Log.i(TAG, "OnPre");
super.onPreExecute();
pd=new ProgressDialog(c);
pd.setTitle("En Proceso");
pd.setMessage("Procesando datos...Espere por favor");
pd.show();
}
/*
1.WHERE WE SEND DATA TO NETWORK
2.RETURNS FOR US A STRING
*/
@Override
protected String doInBackground(Void... params) {
Log.i(TAG, "doInBack");
return this.send();
}
/*
1. CALLED WHEN JOB IS OVER
2. WE DISMISS OUR PD
3.RECEIVE A STRING FROM DOINBACKGROUND
*/
@Override
protected void onPostExecute(String response) {
Log.i(TAG, "onPost");
super.onPostExecute(response);
MainActivity.ResponseWeb = response;

if (MainActivity.ResponseWeb != null) {
//...
//HERE CODE DO SOME STUFF
//...
}
else {
Toast.makeText(c, "Error: response " + MainActivity.ResponseWeb , Toast.LENGTH_SHORT).show();
}
pd.dismiss();
}


/*
SEND DATA OVER THE NETWORK
RECEIVE AND RETURN A RESPONSE
*/
private String send()
{
Log.i(TAG, "On Send 1");
//CONNECT
HttpURLConnection con=Connector.connect(urlAddress);
Log.i(TAG, "On Send 2");
if(con==null)
{
Log.i(TAG, "On Send 3");
return null;
}
try
{
Log.i(TAG, "On Send 4");
OutputStream os=con.getOutputStream();
Log.i(TAG, "On Send 5");
//WRITE
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
Log.i(TAG, "On Send 6");
bw.flush();
Log.i(TAG, "On Send 7");
//RELEASE RES
bw.close();
Log.i(TAG, "On Send 8");
os.close();
Log.i(TAG, "On Send 9");
//HAS IT BEEN SUCCESSFUL?
int responseCode=con.getResponseCode();
Log.i(TAG, "On Send 10");
Log.i(TAG, "responseCode = " + responseCode);
if(responseCode==con.HTTP_OK)
{
Log.i(TAG, "On Send 11");
//GET EXACT RESPONSE
BufferedReader br=new BufferedReader(new InputStreamReader(con.getInputStream()));
Log.i(TAG, "On Send 12");
StringBuffer response=new StringBuffer();
Log.i(TAG, "On Send 13");
String line;
Log.i(TAG, "On Send 14");
//READ LINE BY LINE
while ((line=br.readLine()) != null)
{
Log.i(TAG, "On Send 15");
response.append(line);
}
//RELEASE RES
Log.i(TAG, "On Send 16");
br.close();
Log.i(TAG, "On Send 17");
return response.toString();
}else
{
Log.i(TAG, "On Send 18");
}
} catch (IOException e) {
Log.i(TAG, "On Send 19");
e.printStackTrace();
}
Log.i(TAG, "On Send 20");
return null;
}

这是 logcat:

04-04 15:34:01.402 20341-20341/com.example.pruebainsp I/Sender: OnPre
04-04 15:34:01.445 20341-20550/com.example.pruebainsp I/Sender: doInBack
04-04 15:34:01.445 20341-20550/com.example.pruebainsp I/Sender: On Send 1
04-04 15:34:01.445 20341-20550/com.example.pruebainsp I/Sender: On Send 2
04-04 15:34:01.446 20341-20550/com.example.pruebainsp I/Sender: On Send 4
04-04 15:34:01.561 20341-20550/com.example.pruebainsp I/Sender: On Send 5
04-04 15:34:01.562 20341-20550/com.example.pruebainsp I/Sender: On Send 6
04-04 15:34:01.562 20341-20550/com.example.pruebainsp I/Sender: On Send 7
04-04 15:34:01.562 20341-20550/com.example.pruebainsp I/Sender: On Send 8
04-04 15:34:01.562 20341-20550/com.example.pruebainsp I/Sender: On Send 9
04-04 15:34:01.700 20341-20550/com.example.pruebainsp I/Sender: On Send 10
04-04 15:53:57.934 27226-27436/com.example.pruebainsp I/Sender: responseCode = 403
04-04 15:34:01.700 20341-20550/com.example.pruebainsp I/Sender: On Send 18
04-04 15:34:01.700 20341-20550/com.example.pruebainsp I/Sender: On Send 20
04-04 15:34:02.214 20341-20341/com.example.pruebainsp I/Sender: onPost

最佳答案

doInBackground() 顾名思义在后台线程中执行任务。理想情况下,您不能在后台线程上执行 UI 相关操作。一些三星设备倾向于允许这样做,这很奇怪。在某些手机上,它会直接导致您的应用崩溃。

为了使其适用于所有设备,您可以:

@Override
protected Object doInBackground(Object[] objects) {
activityObj.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(contet, "Hello", Toast.LENGTH_LONG).show();
}
});
return null;
}

@Override
protected Object doInBackground(Object[] objects) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_LONG).show();
}
});
}

关于java - Android AsyncTask doInBackground 适用于 1 部手机,但不适用于其他手机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49653420/

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