gpt4 book ai didi

java - Android AsyncTask 订单服务器连接

转载 作者:太空宇宙 更新时间:2023-11-04 11:22:39 24 4
gpt4 key购买 nike

我目前正在使用 Android Studio 开发一个项目。为了使用套接字完成一些操作,我必须使用 AsyncTask。我正在尝试获取我的数据库中可用的补充剂列表。之后,客户选择其中一些补充品并确认。更改 Activity 是为了计算命令的总价。

This AsyncTask gets the Supplements in the database (works very well)

public class suppTask extends AsyncTask<Void, Void, Boolean> {

public ArrayList<Dessert> listDessert;

suppTask() {
this.listDessert=new ArrayList<Dessert>();
}


@Override
public Boolean doInBackground(Void... params) {
// TODO: attempt authentication against a network service.
Requester client = new Requester();

ArrayList<Dessert> desserts = client.getDessert();
ArrayList<Opening> openings= client.getOpening();
ArrayList<Drinks> drinks = client.getDrink();
ArrayList<Snacks> snacks = client.getSnacks();
Menu menu = client.getMenu(type,num);

//All Supplements.something references to static Arraylist or obj
Supplement.d=desserts;
Supplement.o=openings;
Supplement.s=snacks;
Supplement.d=drinks;
Supplement.m= menu;

return true;
}

An example of one of the get Functions (following the same patern, perfectly fine) :

ArrayList<Dessert> getDessert()
{
connectToServer(); //Connection to the server //with static attributes

ArrayList<Dessert> listDessert = new ArrayList<Dessert>();
JSONArray jArray = new JSONArray();
String s="";
try {

this.message = (String)this.in.readObject(); //message receive from server

this.sendMessage("askDessert"); //message sent to server

s = (String) this.in.readObject(); //received from server
jArray=strToJson(s);

this.message = "bye";
this.sendMessage(this.message);

} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
finally {
this.stopConnection(); //Disconnect the stream
}


listDessert=fromJSONDessert(jArray);

return listDessert;
}

The second AsyncTask (calculates the total price, works well) :

public static class totalTask extends AsyncTask<Void, Void, Boolean> {

@Override
public Boolean doInBackground(Void... params) {
Requester client = new Requester();
total = client.getTotal("1:0:0:0:0"); //Ids of every Supplement in the dataBase
return true;
}

}

How we open the connection (used for every getters) :

    void connectToServer()
{
try {
this.requestSocket=new Socket(host,port);


this.out = new ObjectOutputStream(this.requestSocket.getOutputStream());
this.out.flush();


this.in = new ObjectInputStream(this.requestSocket.getInputStream());


} catch (IOException e) {
e.printStackTrace();

}

}

How we stop the connection (used for every getters)

 void stopConnection()
{
try {
this.in.close();
this.out.close();
this.requestSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}

The problem is that both AsyncTask works very well alone in a specific order. For example, when we call them like this IT WORKS :

    OtherActivity.totalTask to = new OtherActivity.totalTask();
to.execute();

suppTask t = new suppTask();
t.execute();

The thing is that we need to call them in the opposite way (suppTask first and then, when this first task is finished, and the activity has been changed, the task totalTask is called). When we do this, in the second AsyncTask the function getTotal crashes at the beginning when the function connectToServer() is called. It blocks at this line (never reached) :

this.in = new ObjectInputStream(this.requestSocket.getInputStream());

So how do we solve this ? Thank you for your kindness and time.

最佳答案

它们是独立的异步任务。您需要将第二个请求放在 onPostExecute() 主体中。

关于java - Android AsyncTask 订单服务器连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44738533/

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