【发布时间】:2018-04-04 14:17:07
【问题描述】:
我有一个应用程序在我的三星 Galaxy S7 版本 7.0 API 24 中运行良好。但是当我在华为 CAM-L03 版本 6.0 API 23 中尝试它时它不起作用。
基本上我有这样的课程:
公共类 Sender 扩展 AsyncTask {...}
具有 onPreExecute、doInBackground 和 onPostExecute 方法,用于通过 Internet 与 php 文件进行通信。 当我在三星 Galaxy S7 上运行它并执行 Toast.makeText 时,以各种方式在屏幕上显示文本,它运行得非常好。 但是当我在华为 CAM-L03 中运行它时,只显示来自 onPreExecute 和 onPostExecute 的文本,但从未进入 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 消息并不是真正的问题.. 我只是为视图添加它们,只是为了检查方法。真正的麻烦是 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
【问题讨论】:
-
“Sender”类的代码比“build.gradle”文件更有帮助。请添加代码
-
如何在 doInBackground 中显示 toast?您是否在主线程中调用
show()(如runOnUiThread)?也许这个答案可能会有所帮助:stackoverflow.com/a/3134720/4245651 -
我什至很惊讶它允许在
doInBackground中执行Toast.makeTest,因为您不在 UI 线程上。所以我会说华为的行为是正确的。如果您想知道该方法是否被调用,请执行Log.d("name", "message");或放置断点而不是显示 Toast -
toast.makeText 并不是真正的问题。出于这个原因,我编辑了这个问题。
标签: java android android-asynctask sdk