gpt4 book ai didi

android - CountDownLatch 使应用程序停止

转载 作者:行者123 更新时间:2023-11-29 23:45:35 27 4
gpt4 key购买 nike

我正在处理 CountDownLatch,我需要发送两个 JSON 以按顺序发送。

我想出了这个

   CountDownLatch countDownLatch = new CountDownLatch(1);
try {
sendAndgetEODID(countDownLatch);
countDownLatch.await();
sendAndgetDispID();
} catch (InterruptedException e) {
e.printStackTrace();
}

sendAndGetEODID 函数的内部是

private void sendAndgetEODID(final CountDownLatch countDownLatch) {

Log.e("going to EOD", "going to EOD");

JSONObject params = new JSONObject();

try {
params.put("company_id", CompanyID);
params.put("subcompany_id", subCompID);
params.put("control_no", control_no);
params.put("trip_no", trip_no);
params.put("bus_id", bus_id);
params.put("conductor_id", conductor_id);
params.put("driver_id", driver_id);
params.put("driver2_id", null); //temporary
params.put("device_serial", dev_serial);
params.put("datetimestamp", datetimestamp);

String baseUrl = null;

final ConnectivityManager connMgr = (ConnectivityManager)
mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

if (wifi.isConnected()) {
baseUrl = url1;
} else if (mobile.isConnected()) {
baseUrl = url2;
}

AndroidNetworking.post(baseUrl + "atts_eods")
.addJSONObjectBody(params)
.addHeaders("Content-Type", "application/json")
.addHeaders("Prefer", "return=representation")
.addHeaders("Accept", "application/vnd.pgrst.object+json")
.setPriority(Priority.HIGH)
.build()
.getAsJSONObject(new JSONObjectRequestListener() {

@Override
public void onResponse(JSONObject response) {
Log.e("response EOD", String.valueOf(response));

try {
eod_return_id = String.valueOf(response.get("id"));

String queryEOD = ("update " + DBHelper.TRANS_EOD + " set " +
DBHelper.TRANS_EOD_RETURN_ID + " = '" + eod_return_id +
"', is_send = '1' where control_no = '" + control_no + "'");
sqldb.execSQL(queryEOD);

String updateTransSettingsEODID = ("update " + DBHelper.TRANS_SETTINGS + " set " +
DBHelper.TRANS_EOD_RETURN_ID + " = '" + eod_return_id +
"' where control_no = '" + control_no + "'");

sqldb.execSQL(updateTransSettingsEODID);

String updateEODIDDispatch = ("update " + DBHelper.TRANS_DISPATCH + " set " +
DBHelper.TRANS_DISPATCH_EOD_ID + " = '" + eod_return_id +
"' where control_no = '" + control_no + "' and trip_no = '" + trip_no + "' ");

sqldb.execSQL(updateEODIDDispatch);


EODIDCatcher = eod_return_id;
Log.e("EODIDCatcher UP!", EODIDCatcher);

countDownLatch.countDown();

} catch (JSONException e) {
e.printStackTrace();
countDownLatch.countDown();
}
}

@Override
public void onError(ANError anError) {

Log.e("anError", "sending failed, it will generate a EODID222 \n" + anError.getErrorBody());

EODIDCatcher = generatedEODID;

String queryEOD = ("update " + DBHelper.TRANS_EOD + " set " + DBHelper.TRANS_EOD_RETURN_ID + " = '" + generatedEODID + "',is_send = '2' where control_no = '" + control_no + "' and trip_no = '" + trip_no + "' ");
sqldb.execSQL(queryEOD);

String updateEODIDDispatch = ("update " + DBHelper.TRANS_DISPATCH + " set " + DBHelper.TRANS_DISPATCH_EOD_ID + " = '" + generatedEODID + "' where control_no = '" + control_no + "' and trip_no = '" + trip_no + "' ");
sqldb.execSQL(updateEODIDDispatch);

String updateTransSettingsEODID = ("update " + DBHelper.TRANS_SETTINGS + " set " + DBHelper.TRANS_EOD_RETURN_ID + " = '" + generatedEODID + "' where control_no = '" + control_no + "' and trip_no = '" + trip_no + "' ");
sqldb.execSQL(updateTransSettingsEODID);


Log.e("EODIDCatcher ERROR", EODIDCatcher);
countDownLatch.countDown();
}
});
} catch (Throwable e) {
e.printStackTrace();
}
}

我可以知道错误在哪里吗?而且它没有执行我的第二个功能?谢谢!

最佳答案

它被阻止是因为您的 sendAndgetEODID(countDownLatch); 不会运行完成。 await 导致当前线程等待,直到锁存器倒计时到零。当您收到响应(异步)时,countDownLatch.await(); 将被触发(因为它是同步的)并导致线程等待。

解决方法:

方法一:

如果您打算在 sendAndgetEODID(countDownLatch); 调用执行后执行 sendAndgetDispID();,则只需替换 countDownLatch.countDown();sendAndgetDispID(); 如下:

new JSONObjectRequestListener() {

@Override
public void onResponse(JSONObject response) {
Log.e("response EOD", String.valueOf(response));

try {
...
sendAndgetDispID();

} catch (JSONException e) {
e.printStackTrace();
sendAndgetDispID();
}
}

@Override
public void onError(ANError anError) {

...
sendAndgetDispID();
}
});

方法 2:

在不同线程中处理 AndroidNetworking.post。这可能会导致一些多线程问题,因此您需要小心并有效地使用同步块(synchronized block)。

关于android - CountDownLatch 使应用程序停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51470754/

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