Android 无法同时运行多个异步任务
我正在编写一个应用程序,它使用类似Android手机的客户端,并通过TCP套接字连接到java服务器 我的问题是:我使用一个服务向java服务器发送/接收消息,并使用asynctask来保持连接,但当我需要发送请求并等待服务器的响应时,我使用另一个asynctask来执行此操作,但第二个asynctask无法运行。 这是我的密码 服务器中的异步任务(连接-保留接收方消息)Android 无法同时运行多个异步任务,android,tcp,android-asynctask,Android,Tcp,Android Asynctask,我正在编写一个应用程序,它使用类似Android手机的客户端,并通过TCP套接字连接到java服务器 我的问题是:我使用一个服务向java服务器发送/接收消息,并使用asynctask来保持连接,但当我需要发送请求并等待服务器的响应时,我使用另一个asynctask来执行此操作,但第二个asynctask无法运行。 这是我的密码 服务器中的异步任务(连接-保留接收方消息) 公共类connectTask扩展了异步任务{ @凌驾 受保护的TCPClient doInBackground(字符串…消息
公共类connectTask扩展了异步任务{
@凌驾
受保护的TCPClient doInBackground(字符串…消息){
Log.d(TAG1,“连接任务-在asycn任务-3中”);
//我们创建一个TCPClient对象并
mmTcpClient=新的TCPClient(新的TCPClient.OnMessageReceived(){
@凌驾
//这里实现了messageReceived方法
已接收公共无效消息(字符串消息){
//此方法调用onProgressUpdate
Log.d(TAG1,“连接任务-在asycn任务-4中”);
出版进度(信息);
}
});
if(LocalData.isConnectsuccess==false)
{
试一试{
Log.d(TAG1,“连接任务-在asycn任务-5中”);
mmTcpClient.run(“172.16.10.37”,44444);
Log.d(TAG1,“在asycn任务-6中启动的服务”);
}
捕获(例外e)
{
Log.e(TAG1,“+e);
}
}
返回null;
}
@凌驾
受保护的void onProgressUpdate(最终字符串…值){
super.onProgressUpdate(值);
Log.e(TAG1,“Onprogressupdate”+值[0]);
LocalData.strreceiver=值[0];
Intent intt=新的Intent(ConnectService.this、Customdialog.class);
intt.putExtra(“mess”,LocalData.strreceiver);
intt.addFlags(意图、标志、活动、新任务);
起始触觉(intt);
}
}
和用于登录活动的第二个异步任务
class ASlogin extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(SigninActivity.this);
pDialog.setMessage("Logging in");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
Log.d(Tag2, "pdialog show");
}
/**
* getting All products from url
* */
protected String doInBackground(String... message) {
// Building Parameters
Log.d(Tag2, "Doinbackground");
try {
if (LocalData.isConnectsuccess == true) {
Log.d(Tag2, "Send-1");
JSONObject jslogin = new JSONObject();
try {
jslogin.put("tag","login");
jslogin.put("email", edtEmailSignIn.getText().toString());
jslogin.put("password", edtPasswordSignIn.getText()
.toString());
Log.d(Tag2, "Put Json-2");
} catch (JSONException e) {
Log.e(Tag2, "JSON failed" + e);
}
mTcpClient.sendMessage(jslogin.toString());
Log.d(Tag2, "JsonString: " + jslogin.toString());
}
else {
Log.e(Tag2, "Connect not success" + LocalData.isConnectsuccess);
}
} catch (Exception e) {
Log.e(Tag2, "Fail parse Json" + e);
}
return null;
}
@Override
protected void onProgressUpdate(String... message) {
super.onProgressUpdate(message);
pDialog.dismiss();
OJResponsive strreturn = new OJResponsive();
try{
strreturn = JSonparse.getrespon(LocalData.strreceiver);
}
catch (Exception e)
{
alertmess("Login fail" + e);
Log.e(Tag2,"Json parse failed"+e);
}
if (strreturn.getResult()=="success")
{
Toast.makeText(SigninActivity.this, "Login success", Toast.LENGTH_SHORT).show();
Intent inhctr = new Intent(SigninActivity.this,HomeControlActivity.class);
startActivity(inhctr);
finish();
}
else alertmess("Login fail");
Log.e(Tag2,"Login fail"+e);
}
}
类ASlogin扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(signianctivity.this);
pDialog.setMessage(“登录”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
Log.d(Tag2,“pdialog show”);
}
/**
*从url获取所有产品
* */
受保护的字符串doInBackground(字符串…消息){
//建筑参数
Log.d(Tag2,“Doinbackground”);
试一试{
if(LocalData.isConnectsuccess==true){
日志d(Tag2,“发送-1”);
JSONObject jslogin=new JSONObject();
试一试{
jslogin.put(“标记”、“登录”);
jslogin.put(“email”,edtEmailSignIn.getText().toString());
jslogin.put(“密码”,edtpasswordsign.getText())
.toString());
Log.d(Tag2,“Put-Json-2”);
}捕获(JSONException e){
Log.e(Tag2,“JSON失败”+e);
}
mTcpClient.sendMessage(jslogin.toString());
d(Tag2,“JsonString:+jslogin.toString());
}
否则{
Log.e(Tag2,“连接不成功”+LocalData.isConnectsuccess);
}
}捕获(例外e){
Log.e(Tag2,“解析Json失败”+e);
}
返回null;
}
@凌驾
受保护的void onProgressUpdate(字符串…消息){
super.onProgressUpdate(消息);
pDialog.disclose();
OJResponsive strreturn=新的OJResponsive();
试一试{
strreturn=JSonparse.getrespon(LocalData.strreceiver);
}
捕获(例外e)
{
alertmess(“登录失败”+e);
Log.e(Tag2,“Json解析失败”+e);
}
if(strreturn.getResult()=“成功”)
{
Toast.makeText(SigninActivity.this,“登录成功”,Toast.LENGTH_SHORT.show();
Intent inhctr=新意图(SigninActivity.this,HomeControlActivity.class);
星触觉(inhctr);
完成();
}
else alertmess(“登录失败”);
Log.e(Tag2,“登录失败”+e);
}
}
第二个AsycntTask仅在onPreExecute()处运行,并在showprocessdialog处停止。
有什么可以帮我的吗?在这种情况下,还有更好的解决方案吗?
谢谢。不同版本的android对异步任务执行的处理方式不同 看看这个答案:尝试使用
AsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
为此,请使用executor。使用AsyncTask.executeOnExecutor(AsyncTask.THREAD\u POOL\u EXECUTOR)运行第二个AsyncTask到doinbackground;它不能跳入onPostExecute,你能帮我吗?第二个异步任务中没有onPostExecute
AsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);