Android Internet 服务工作请求 运行 并行
Android Intent Service work requests running parallely
我创建了一个 IntentService 如下:
public class OrdersSyncService extends IntentService {
private static final String TAG = OrdersSyncService.class.getSimpleName();
private Order orderObject;
public OrdersSyncService() {
super("OrdersSyncService");
}
@Override
protected void onHandleIntent(Intent intent) {
//Util.showToast(getApplicationContext(), "Syncing Orders........");
Log.d(TAG, "I'm running....");
syncOrders();
}
private void syncOrders() {
Database database = CouchBaseHelper.openCouchBaseDB(this);
JSONArray ordersJsonArray = new JSONArray(CouchBaseHelper.retrieveNotSyncedOrders(database));
if (ordersJsonArray.length() != 0) {
uploadOrders(ordersJsonArray.toString());
Log.d(TAG, "Orders syncing started.");
} else {
Log.d(TAG, "Nothing to sync.");
}
}
private void uploadOrders(String ordersJsonArray) {
RetrofitApi.ApiInterface apiInterface = RetrofitApi.getApiInterfaceInstance();
final Call<Order> uploadOrders = apiInterface.uploadOrders(
ordersJsonArray,
Util.getDeviceId(this),
AppPreference.getString(this, AppPreference.USER_ID)
);
uploadOrders.enqueue(new Callback<Order>() {
@Override
public void onResponse(Call<Order> call, Response<Order> response) {
if (response.isSuccessful()) {
if (response.body().getStatus().equals("success")) {
orderObject = response.body();
Log.d(TAG, "Server Response : " + orderObject.getMobileOrderIds());
boolean flag = updateOrders(orderObject);
if (flag) {
Log.d(TAG, "Orders Synced And Updated.");
EventBus.getDefault().post(new SyncFinished(true));
} else {
Log.d(TAG, "Orders Synced But Update Failed.");
}
} else {
Log.d(TAG, response.body().getMessage());
}
} else {
Log.d(TAG, "Some Error. Sync Failed.");
}
}
@Override
public void onFailure(Call<Order> call, Throwable t) {
Log.d(TAG, "Network problem. Sync Failed.");
//Log.d("VICKY",call.request().url().toString());
}
});
}
private boolean updateOrders(Order order) {
Database database = CouchBaseHelper.openCouchBaseDB(this);
boolean flag = CouchBaseHelper.updateOrders(database, order);
return flag;
}
}
它所做的是发出网络请求,并在网络请求后更新数据库(我使用 couchbaselite android)和响应。现在的问题是当我向它发出多个请求时它会并行执行。 android 文档明确指出:
Creating a Background Service
Work requests run sequentially. If an operation is running in an IntentService, and you send it another request, the request waits until the first operation is finished.
我从 Activity 的 onResume
方法调用此服务。因此,每当 activity 恢复时,都会调用此服务:
@Override
protected void onResume() {
super.onResume();
Intent ordersSyncServiceIntent = new Intent(SellerHomeActivity.this, OrdersSyncService.class);
startService(ordersSyncServiceIntent);
}
我不知道是什么问题。我认为这与我用来发出异步网络请求的 Retrofit
库有关。请帮忙。
您在这一行异步执行:
uploadOrders.enqueue(new Callback<Order>() {...});
使用execute()方法同步完成。
我创建了一个 IntentService 如下:
public class OrdersSyncService extends IntentService {
private static final String TAG = OrdersSyncService.class.getSimpleName();
private Order orderObject;
public OrdersSyncService() {
super("OrdersSyncService");
}
@Override
protected void onHandleIntent(Intent intent) {
//Util.showToast(getApplicationContext(), "Syncing Orders........");
Log.d(TAG, "I'm running....");
syncOrders();
}
private void syncOrders() {
Database database = CouchBaseHelper.openCouchBaseDB(this);
JSONArray ordersJsonArray = new JSONArray(CouchBaseHelper.retrieveNotSyncedOrders(database));
if (ordersJsonArray.length() != 0) {
uploadOrders(ordersJsonArray.toString());
Log.d(TAG, "Orders syncing started.");
} else {
Log.d(TAG, "Nothing to sync.");
}
}
private void uploadOrders(String ordersJsonArray) {
RetrofitApi.ApiInterface apiInterface = RetrofitApi.getApiInterfaceInstance();
final Call<Order> uploadOrders = apiInterface.uploadOrders(
ordersJsonArray,
Util.getDeviceId(this),
AppPreference.getString(this, AppPreference.USER_ID)
);
uploadOrders.enqueue(new Callback<Order>() {
@Override
public void onResponse(Call<Order> call, Response<Order> response) {
if (response.isSuccessful()) {
if (response.body().getStatus().equals("success")) {
orderObject = response.body();
Log.d(TAG, "Server Response : " + orderObject.getMobileOrderIds());
boolean flag = updateOrders(orderObject);
if (flag) {
Log.d(TAG, "Orders Synced And Updated.");
EventBus.getDefault().post(new SyncFinished(true));
} else {
Log.d(TAG, "Orders Synced But Update Failed.");
}
} else {
Log.d(TAG, response.body().getMessage());
}
} else {
Log.d(TAG, "Some Error. Sync Failed.");
}
}
@Override
public void onFailure(Call<Order> call, Throwable t) {
Log.d(TAG, "Network problem. Sync Failed.");
//Log.d("VICKY",call.request().url().toString());
}
});
}
private boolean updateOrders(Order order) {
Database database = CouchBaseHelper.openCouchBaseDB(this);
boolean flag = CouchBaseHelper.updateOrders(database, order);
return flag;
}
}
它所做的是发出网络请求,并在网络请求后更新数据库(我使用 couchbaselite android)和响应。现在的问题是当我向它发出多个请求时它会并行执行。 android 文档明确指出: Creating a Background Service
Work requests run sequentially. If an operation is running in an IntentService, and you send it another request, the request waits until the first operation is finished.
我从 Activity 的 onResume
方法调用此服务。因此,每当 activity 恢复时,都会调用此服务:
@Override
protected void onResume() {
super.onResume();
Intent ordersSyncServiceIntent = new Intent(SellerHomeActivity.this, OrdersSyncService.class);
startService(ordersSyncServiceIntent);
}
我不知道是什么问题。我认为这与我用来发出异步网络请求的 Retrofit
库有关。请帮忙。
您在这一行异步执行:
uploadOrders.enqueue(new Callback<Order>() {...});
使用execute()方法同步完成。