ArrayList iterator.remove() inside AsyncHttpClient throws IllegalStateException
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am trying to do HttpClient POST to submit each item of an ArrayList one by one to the server. Here are some snippets of my codes:
ArrayList<String> testArrayList = new ArrayList<>();
testArrayList.add("Item 1");
testArrayList.add("Item 2");
final Iterator<String> iterator = testArrayList.iterator();
while(iterator.hasNext()){
for(int i = 0; i<testArrayList.size(); i++){
currentContent += "| " + testArrayList.get(i) + " | ";
}
currentItem = iterator.next();
final String url = "some url here"
RequestParams params = new RequestParams();
params.add("item_name", currentItem);
new AsyncHttpClient().post(url, params, new AsyncHttpResponseHandler( {
@Override
public void onSuccess(int statusCode, Header headers, byte responseBody) {
Toast.makeText(context, "Removing..", Toast.LENGTH_SHORT).show();
iterator.remove();
}
@Override
public void onFailure(int statusCode, Header headers, byte responseBody, Throwable error) {
}
});
}
The problem is iterator.remove()
will always throw IllegalStateException. It doesn't happen if AsyncHttpClient is not involved. What I've tried:
- I've tried putting
iterator.next()
withinonSuccess()
(noRequestParams
added in this case, just to try it out) but it
causes the app to stuck and not responding. (In my case I actually
would like to keep it outsideonSuccess()
because from the iterator
is where I will retrieve myRequestParams
) - I've searched for similar questions. Most of the problems are caused by not calling
iterator.next()
beforeiterator.remove()
. I have it but my code is still not working. Another frequent cause would be theiterator.remove()
is called within an inner loop, but that is not my case.
I've been stuck at this problem for weeks. Please help me out.
java android arraylist iterator asynchttpclient
add a comment |
I am trying to do HttpClient POST to submit each item of an ArrayList one by one to the server. Here are some snippets of my codes:
ArrayList<String> testArrayList = new ArrayList<>();
testArrayList.add("Item 1");
testArrayList.add("Item 2");
final Iterator<String> iterator = testArrayList.iterator();
while(iterator.hasNext()){
for(int i = 0; i<testArrayList.size(); i++){
currentContent += "| " + testArrayList.get(i) + " | ";
}
currentItem = iterator.next();
final String url = "some url here"
RequestParams params = new RequestParams();
params.add("item_name", currentItem);
new AsyncHttpClient().post(url, params, new AsyncHttpResponseHandler( {
@Override
public void onSuccess(int statusCode, Header headers, byte responseBody) {
Toast.makeText(context, "Removing..", Toast.LENGTH_SHORT).show();
iterator.remove();
}
@Override
public void onFailure(int statusCode, Header headers, byte responseBody, Throwable error) {
}
});
}
The problem is iterator.remove()
will always throw IllegalStateException. It doesn't happen if AsyncHttpClient is not involved. What I've tried:
- I've tried putting
iterator.next()
withinonSuccess()
(noRequestParams
added in this case, just to try it out) but it
causes the app to stuck and not responding. (In my case I actually
would like to keep it outsideonSuccess()
because from the iterator
is where I will retrieve myRequestParams
) - I've searched for similar questions. Most of the problems are caused by not calling
iterator.next()
beforeiterator.remove()
. I have it but my code is still not working. Another frequent cause would be theiterator.remove()
is called within an inner loop, but that is not my case.
I've been stuck at this problem for weeks. Please help me out.
java android arraylist iterator asynchttpclient
Why you are doing all Async calls insidewhile(iterator.hasNext())
?iterator.remove()
insideonSuccess
is causing the problem . Since its an Asynchronous call thats whyiterator.remove()
is throwingIllegalStateException
You should look for a elegant way to do this.
– ADM
Jan 3 at 4:20
@ADM I am sorry for my unelegant way as I am still learning android. What i'm thinking is: after successfully sending the arraylist by HTTP POST, I want it to be removed from the arraylist. That's whyremove()
is called insideonSuccess
. What other way can I do it?
– tyn
Jan 3 at 6:15
add a comment |
I am trying to do HttpClient POST to submit each item of an ArrayList one by one to the server. Here are some snippets of my codes:
ArrayList<String> testArrayList = new ArrayList<>();
testArrayList.add("Item 1");
testArrayList.add("Item 2");
final Iterator<String> iterator = testArrayList.iterator();
while(iterator.hasNext()){
for(int i = 0; i<testArrayList.size(); i++){
currentContent += "| " + testArrayList.get(i) + " | ";
}
currentItem = iterator.next();
final String url = "some url here"
RequestParams params = new RequestParams();
params.add("item_name", currentItem);
new AsyncHttpClient().post(url, params, new AsyncHttpResponseHandler( {
@Override
public void onSuccess(int statusCode, Header headers, byte responseBody) {
Toast.makeText(context, "Removing..", Toast.LENGTH_SHORT).show();
iterator.remove();
}
@Override
public void onFailure(int statusCode, Header headers, byte responseBody, Throwable error) {
}
});
}
The problem is iterator.remove()
will always throw IllegalStateException. It doesn't happen if AsyncHttpClient is not involved. What I've tried:
- I've tried putting
iterator.next()
withinonSuccess()
(noRequestParams
added in this case, just to try it out) but it
causes the app to stuck and not responding. (In my case I actually
would like to keep it outsideonSuccess()
because from the iterator
is where I will retrieve myRequestParams
) - I've searched for similar questions. Most of the problems are caused by not calling
iterator.next()
beforeiterator.remove()
. I have it but my code is still not working. Another frequent cause would be theiterator.remove()
is called within an inner loop, but that is not my case.
I've been stuck at this problem for weeks. Please help me out.
java android arraylist iterator asynchttpclient
I am trying to do HttpClient POST to submit each item of an ArrayList one by one to the server. Here are some snippets of my codes:
ArrayList<String> testArrayList = new ArrayList<>();
testArrayList.add("Item 1");
testArrayList.add("Item 2");
final Iterator<String> iterator = testArrayList.iterator();
while(iterator.hasNext()){
for(int i = 0; i<testArrayList.size(); i++){
currentContent += "| " + testArrayList.get(i) + " | ";
}
currentItem = iterator.next();
final String url = "some url here"
RequestParams params = new RequestParams();
params.add("item_name", currentItem);
new AsyncHttpClient().post(url, params, new AsyncHttpResponseHandler( {
@Override
public void onSuccess(int statusCode, Header headers, byte responseBody) {
Toast.makeText(context, "Removing..", Toast.LENGTH_SHORT).show();
iterator.remove();
}
@Override
public void onFailure(int statusCode, Header headers, byte responseBody, Throwable error) {
}
});
}
The problem is iterator.remove()
will always throw IllegalStateException. It doesn't happen if AsyncHttpClient is not involved. What I've tried:
- I've tried putting
iterator.next()
withinonSuccess()
(noRequestParams
added in this case, just to try it out) but it
causes the app to stuck and not responding. (In my case I actually
would like to keep it outsideonSuccess()
because from the iterator
is where I will retrieve myRequestParams
) - I've searched for similar questions. Most of the problems are caused by not calling
iterator.next()
beforeiterator.remove()
. I have it but my code is still not working. Another frequent cause would be theiterator.remove()
is called within an inner loop, but that is not my case.
I've been stuck at this problem for weeks. Please help me out.
java android arraylist iterator asynchttpclient
java android arraylist iterator asynchttpclient
asked Jan 3 at 4:12
tyntyn
85112
85112
Why you are doing all Async calls insidewhile(iterator.hasNext())
?iterator.remove()
insideonSuccess
is causing the problem . Since its an Asynchronous call thats whyiterator.remove()
is throwingIllegalStateException
You should look for a elegant way to do this.
– ADM
Jan 3 at 4:20
@ADM I am sorry for my unelegant way as I am still learning android. What i'm thinking is: after successfully sending the arraylist by HTTP POST, I want it to be removed from the arraylist. That's whyremove()
is called insideonSuccess
. What other way can I do it?
– tyn
Jan 3 at 6:15
add a comment |
Why you are doing all Async calls insidewhile(iterator.hasNext())
?iterator.remove()
insideonSuccess
is causing the problem . Since its an Asynchronous call thats whyiterator.remove()
is throwingIllegalStateException
You should look for a elegant way to do this.
– ADM
Jan 3 at 4:20
@ADM I am sorry for my unelegant way as I am still learning android. What i'm thinking is: after successfully sending the arraylist by HTTP POST, I want it to be removed from the arraylist. That's whyremove()
is called insideonSuccess
. What other way can I do it?
– tyn
Jan 3 at 6:15
Why you are doing all Async calls inside
while(iterator.hasNext())
? iterator.remove()
inside onSuccess
is causing the problem . Since its an Asynchronous call thats why iterator.remove()
is throwing IllegalStateException
You should look for a elegant way to do this.– ADM
Jan 3 at 4:20
Why you are doing all Async calls inside
while(iterator.hasNext())
? iterator.remove()
inside onSuccess
is causing the problem . Since its an Asynchronous call thats why iterator.remove()
is throwing IllegalStateException
You should look for a elegant way to do this.– ADM
Jan 3 at 4:20
@ADM I am sorry for my unelegant way as I am still learning android. What i'm thinking is: after successfully sending the arraylist by HTTP POST, I want it to be removed from the arraylist. That's why
remove()
is called inside onSuccess
. What other way can I do it?– tyn
Jan 3 at 6:15
@ADM I am sorry for my unelegant way as I am still learning android. What i'm thinking is: after successfully sending the arraylist by HTTP POST, I want it to be removed from the arraylist. That's why
remove()
is called inside onSuccess
. What other way can I do it?– tyn
Jan 3 at 6:15
add a comment |
2 Answers
2
active
oldest
votes
You can make use of a recursion where the method calls itself.
ArrayList<String> testArrayList = new ArrayList<>();
testArrayList.add("Item 1");
testArrayList.add("Item 2");
void submitItem(ArrayList<String> items){
final String url = "some url here"
RequestParams params = new RequestParams();
params.add("item_name", currentItem);
new AsyncHttpClient().post(url, params, new AsyncHttpResponseHandler( {
@Override
public void onSuccess(int statusCode, Header headers, byte
responseBody) {
items.remove(0);
Toast.makeText(context, "Removing..", Toast.LENGTH_SHORT).show();
if(ites.size() > 0) submitItem(items);
}
@Override
public void onFailure(int statusCode, Header headers, byte responseBody, Throwable error) {
}
});
}
This actually does the trick. Thank you very much! I've read so much about using arraylist.remove(index) is not very safe with loops but I've never thought of recursion as an alternative.
– tyn
Jan 3 at 7:24
This should work. However, there is an implication of this solution: only single item will be posted at a time. It also does not solve the reliability issue with your design, the "what if my app gets killed" problem.
– Mateusz Mrozewski
Jan 3 at 7:34
add a comment |
From https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html#remove--:
IllegalStateException
- if the next method has not yet been called, or the remove method has already been called after the last call to the next method
You call iterator.remove() from a callback passed to AsyncHttpClient
. That client executes on a different Thread
and callback will be executed on the main UI Thread
. However you have no guarantees about the order of execution here.
So what probably happens time-wise is:
1) iterator.next()
(from the while loop)
2) post triggered
3) iterator.next()
(from the while loop)
4) post triggered
5) first callback iterator.remove()
6) second callback iterator.remove()
You can try to verify it by putting log statements before call to next and remove.
I've added some log statements and yes, you are right. Thank you. However, I still can't figure out how to solve it.
– tyn
Jan 3 at 6:32
@tyn, what exactly are you trying to achieve? Why do you want to remove the items from the ArrayList in the first place?
– Mateusz Mrozewski
Jan 3 at 6:43
The actual ArrayList contains data to be POSTed once internet connection is available. What I'm trying to do was: 1) check if there is internet connection, 2) if there is internet connection, POST data inside ArrayList if any, 3) upon successful submission (onSuccess
), remove the POSTed item from the ArrayList. Think of it as an ArrayList containing pending data created while offline which are to be submitted once device is conected to the internet.
– tyn
Jan 3 at 6:54
1
ArrayList does not seem to be a right choince here as it is kept in memory. What if your app gets killed by OS before internet connection is available? Probably chosing SQLite or some other persistent store will be a better option. Then the solution to your problem will depend on the option you will choose.
– Mateusz Mrozewski
Jan 3 at 7:28
I'm actually using Shared Preference to store the ArrayList as JSON string. Whenever I need to retrieve/save the data, I need to parse the JSON string into ArrayList. I've been reading on SQLite implementation as well and this gives me more reason to actually implement it. Thank you very much.
– tyn
Jan 3 at 7:33
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54016255%2farraylist-iterator-remove-inside-asynchttpclient-throws-illegalstateexception%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can make use of a recursion where the method calls itself.
ArrayList<String> testArrayList = new ArrayList<>();
testArrayList.add("Item 1");
testArrayList.add("Item 2");
void submitItem(ArrayList<String> items){
final String url = "some url here"
RequestParams params = new RequestParams();
params.add("item_name", currentItem);
new AsyncHttpClient().post(url, params, new AsyncHttpResponseHandler( {
@Override
public void onSuccess(int statusCode, Header headers, byte
responseBody) {
items.remove(0);
Toast.makeText(context, "Removing..", Toast.LENGTH_SHORT).show();
if(ites.size() > 0) submitItem(items);
}
@Override
public void onFailure(int statusCode, Header headers, byte responseBody, Throwable error) {
}
});
}
This actually does the trick. Thank you very much! I've read so much about using arraylist.remove(index) is not very safe with loops but I've never thought of recursion as an alternative.
– tyn
Jan 3 at 7:24
This should work. However, there is an implication of this solution: only single item will be posted at a time. It also does not solve the reliability issue with your design, the "what if my app gets killed" problem.
– Mateusz Mrozewski
Jan 3 at 7:34
add a comment |
You can make use of a recursion where the method calls itself.
ArrayList<String> testArrayList = new ArrayList<>();
testArrayList.add("Item 1");
testArrayList.add("Item 2");
void submitItem(ArrayList<String> items){
final String url = "some url here"
RequestParams params = new RequestParams();
params.add("item_name", currentItem);
new AsyncHttpClient().post(url, params, new AsyncHttpResponseHandler( {
@Override
public void onSuccess(int statusCode, Header headers, byte
responseBody) {
items.remove(0);
Toast.makeText(context, "Removing..", Toast.LENGTH_SHORT).show();
if(ites.size() > 0) submitItem(items);
}
@Override
public void onFailure(int statusCode, Header headers, byte responseBody, Throwable error) {
}
});
}
This actually does the trick. Thank you very much! I've read so much about using arraylist.remove(index) is not very safe with loops but I've never thought of recursion as an alternative.
– tyn
Jan 3 at 7:24
This should work. However, there is an implication of this solution: only single item will be posted at a time. It also does not solve the reliability issue with your design, the "what if my app gets killed" problem.
– Mateusz Mrozewski
Jan 3 at 7:34
add a comment |
You can make use of a recursion where the method calls itself.
ArrayList<String> testArrayList = new ArrayList<>();
testArrayList.add("Item 1");
testArrayList.add("Item 2");
void submitItem(ArrayList<String> items){
final String url = "some url here"
RequestParams params = new RequestParams();
params.add("item_name", currentItem);
new AsyncHttpClient().post(url, params, new AsyncHttpResponseHandler( {
@Override
public void onSuccess(int statusCode, Header headers, byte
responseBody) {
items.remove(0);
Toast.makeText(context, "Removing..", Toast.LENGTH_SHORT).show();
if(ites.size() > 0) submitItem(items);
}
@Override
public void onFailure(int statusCode, Header headers, byte responseBody, Throwable error) {
}
});
}
You can make use of a recursion where the method calls itself.
ArrayList<String> testArrayList = new ArrayList<>();
testArrayList.add("Item 1");
testArrayList.add("Item 2");
void submitItem(ArrayList<String> items){
final String url = "some url here"
RequestParams params = new RequestParams();
params.add("item_name", currentItem);
new AsyncHttpClient().post(url, params, new AsyncHttpResponseHandler( {
@Override
public void onSuccess(int statusCode, Header headers, byte
responseBody) {
items.remove(0);
Toast.makeText(context, "Removing..", Toast.LENGTH_SHORT).show();
if(ites.size() > 0) submitItem(items);
}
@Override
public void onFailure(int statusCode, Header headers, byte responseBody, Throwable error) {
}
});
}
answered Jan 3 at 6:45
Ramees ThattarathRamees Thattarath
341312
341312
This actually does the trick. Thank you very much! I've read so much about using arraylist.remove(index) is not very safe with loops but I've never thought of recursion as an alternative.
– tyn
Jan 3 at 7:24
This should work. However, there is an implication of this solution: only single item will be posted at a time. It also does not solve the reliability issue with your design, the "what if my app gets killed" problem.
– Mateusz Mrozewski
Jan 3 at 7:34
add a comment |
This actually does the trick. Thank you very much! I've read so much about using arraylist.remove(index) is not very safe with loops but I've never thought of recursion as an alternative.
– tyn
Jan 3 at 7:24
This should work. However, there is an implication of this solution: only single item will be posted at a time. It also does not solve the reliability issue with your design, the "what if my app gets killed" problem.
– Mateusz Mrozewski
Jan 3 at 7:34
This actually does the trick. Thank you very much! I've read so much about using arraylist.remove(index) is not very safe with loops but I've never thought of recursion as an alternative.
– tyn
Jan 3 at 7:24
This actually does the trick. Thank you very much! I've read so much about using arraylist.remove(index) is not very safe with loops but I've never thought of recursion as an alternative.
– tyn
Jan 3 at 7:24
This should work. However, there is an implication of this solution: only single item will be posted at a time. It also does not solve the reliability issue with your design, the "what if my app gets killed" problem.
– Mateusz Mrozewski
Jan 3 at 7:34
This should work. However, there is an implication of this solution: only single item will be posted at a time. It also does not solve the reliability issue with your design, the "what if my app gets killed" problem.
– Mateusz Mrozewski
Jan 3 at 7:34
add a comment |
From https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html#remove--:
IllegalStateException
- if the next method has not yet been called, or the remove method has already been called after the last call to the next method
You call iterator.remove() from a callback passed to AsyncHttpClient
. That client executes on a different Thread
and callback will be executed on the main UI Thread
. However you have no guarantees about the order of execution here.
So what probably happens time-wise is:
1) iterator.next()
(from the while loop)
2) post triggered
3) iterator.next()
(from the while loop)
4) post triggered
5) first callback iterator.remove()
6) second callback iterator.remove()
You can try to verify it by putting log statements before call to next and remove.
I've added some log statements and yes, you are right. Thank you. However, I still can't figure out how to solve it.
– tyn
Jan 3 at 6:32
@tyn, what exactly are you trying to achieve? Why do you want to remove the items from the ArrayList in the first place?
– Mateusz Mrozewski
Jan 3 at 6:43
The actual ArrayList contains data to be POSTed once internet connection is available. What I'm trying to do was: 1) check if there is internet connection, 2) if there is internet connection, POST data inside ArrayList if any, 3) upon successful submission (onSuccess
), remove the POSTed item from the ArrayList. Think of it as an ArrayList containing pending data created while offline which are to be submitted once device is conected to the internet.
– tyn
Jan 3 at 6:54
1
ArrayList does not seem to be a right choince here as it is kept in memory. What if your app gets killed by OS before internet connection is available? Probably chosing SQLite or some other persistent store will be a better option. Then the solution to your problem will depend on the option you will choose.
– Mateusz Mrozewski
Jan 3 at 7:28
I'm actually using Shared Preference to store the ArrayList as JSON string. Whenever I need to retrieve/save the data, I need to parse the JSON string into ArrayList. I've been reading on SQLite implementation as well and this gives me more reason to actually implement it. Thank you very much.
– tyn
Jan 3 at 7:33
add a comment |
From https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html#remove--:
IllegalStateException
- if the next method has not yet been called, or the remove method has already been called after the last call to the next method
You call iterator.remove() from a callback passed to AsyncHttpClient
. That client executes on a different Thread
and callback will be executed on the main UI Thread
. However you have no guarantees about the order of execution here.
So what probably happens time-wise is:
1) iterator.next()
(from the while loop)
2) post triggered
3) iterator.next()
(from the while loop)
4) post triggered
5) first callback iterator.remove()
6) second callback iterator.remove()
You can try to verify it by putting log statements before call to next and remove.
I've added some log statements and yes, you are right. Thank you. However, I still can't figure out how to solve it.
– tyn
Jan 3 at 6:32
@tyn, what exactly are you trying to achieve? Why do you want to remove the items from the ArrayList in the first place?
– Mateusz Mrozewski
Jan 3 at 6:43
The actual ArrayList contains data to be POSTed once internet connection is available. What I'm trying to do was: 1) check if there is internet connection, 2) if there is internet connection, POST data inside ArrayList if any, 3) upon successful submission (onSuccess
), remove the POSTed item from the ArrayList. Think of it as an ArrayList containing pending data created while offline which are to be submitted once device is conected to the internet.
– tyn
Jan 3 at 6:54
1
ArrayList does not seem to be a right choince here as it is kept in memory. What if your app gets killed by OS before internet connection is available? Probably chosing SQLite or some other persistent store will be a better option. Then the solution to your problem will depend on the option you will choose.
– Mateusz Mrozewski
Jan 3 at 7:28
I'm actually using Shared Preference to store the ArrayList as JSON string. Whenever I need to retrieve/save the data, I need to parse the JSON string into ArrayList. I've been reading on SQLite implementation as well and this gives me more reason to actually implement it. Thank you very much.
– tyn
Jan 3 at 7:33
add a comment |
From https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html#remove--:
IllegalStateException
- if the next method has not yet been called, or the remove method has already been called after the last call to the next method
You call iterator.remove() from a callback passed to AsyncHttpClient
. That client executes on a different Thread
and callback will be executed on the main UI Thread
. However you have no guarantees about the order of execution here.
So what probably happens time-wise is:
1) iterator.next()
(from the while loop)
2) post triggered
3) iterator.next()
(from the while loop)
4) post triggered
5) first callback iterator.remove()
6) second callback iterator.remove()
You can try to verify it by putting log statements before call to next and remove.
From https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html#remove--:
IllegalStateException
- if the next method has not yet been called, or the remove method has already been called after the last call to the next method
You call iterator.remove() from a callback passed to AsyncHttpClient
. That client executes on a different Thread
and callback will be executed on the main UI Thread
. However you have no guarantees about the order of execution here.
So what probably happens time-wise is:
1) iterator.next()
(from the while loop)
2) post triggered
3) iterator.next()
(from the while loop)
4) post triggered
5) first callback iterator.remove()
6) second callback iterator.remove()
You can try to verify it by putting log statements before call to next and remove.
edited Jan 3 at 4:31
ADM
9,159102656
9,159102656
answered Jan 3 at 4:24
Mateusz MrozewskiMateusz Mrozewski
1,83011525
1,83011525
I've added some log statements and yes, you are right. Thank you. However, I still can't figure out how to solve it.
– tyn
Jan 3 at 6:32
@tyn, what exactly are you trying to achieve? Why do you want to remove the items from the ArrayList in the first place?
– Mateusz Mrozewski
Jan 3 at 6:43
The actual ArrayList contains data to be POSTed once internet connection is available. What I'm trying to do was: 1) check if there is internet connection, 2) if there is internet connection, POST data inside ArrayList if any, 3) upon successful submission (onSuccess
), remove the POSTed item from the ArrayList. Think of it as an ArrayList containing pending data created while offline which are to be submitted once device is conected to the internet.
– tyn
Jan 3 at 6:54
1
ArrayList does not seem to be a right choince here as it is kept in memory. What if your app gets killed by OS before internet connection is available? Probably chosing SQLite or some other persistent store will be a better option. Then the solution to your problem will depend on the option you will choose.
– Mateusz Mrozewski
Jan 3 at 7:28
I'm actually using Shared Preference to store the ArrayList as JSON string. Whenever I need to retrieve/save the data, I need to parse the JSON string into ArrayList. I've been reading on SQLite implementation as well and this gives me more reason to actually implement it. Thank you very much.
– tyn
Jan 3 at 7:33
add a comment |
I've added some log statements and yes, you are right. Thank you. However, I still can't figure out how to solve it.
– tyn
Jan 3 at 6:32
@tyn, what exactly are you trying to achieve? Why do you want to remove the items from the ArrayList in the first place?
– Mateusz Mrozewski
Jan 3 at 6:43
The actual ArrayList contains data to be POSTed once internet connection is available. What I'm trying to do was: 1) check if there is internet connection, 2) if there is internet connection, POST data inside ArrayList if any, 3) upon successful submission (onSuccess
), remove the POSTed item from the ArrayList. Think of it as an ArrayList containing pending data created while offline which are to be submitted once device is conected to the internet.
– tyn
Jan 3 at 6:54
1
ArrayList does not seem to be a right choince here as it is kept in memory. What if your app gets killed by OS before internet connection is available? Probably chosing SQLite or some other persistent store will be a better option. Then the solution to your problem will depend on the option you will choose.
– Mateusz Mrozewski
Jan 3 at 7:28
I'm actually using Shared Preference to store the ArrayList as JSON string. Whenever I need to retrieve/save the data, I need to parse the JSON string into ArrayList. I've been reading on SQLite implementation as well and this gives me more reason to actually implement it. Thank you very much.
– tyn
Jan 3 at 7:33
I've added some log statements and yes, you are right. Thank you. However, I still can't figure out how to solve it.
– tyn
Jan 3 at 6:32
I've added some log statements and yes, you are right. Thank you. However, I still can't figure out how to solve it.
– tyn
Jan 3 at 6:32
@tyn, what exactly are you trying to achieve? Why do you want to remove the items from the ArrayList in the first place?
– Mateusz Mrozewski
Jan 3 at 6:43
@tyn, what exactly are you trying to achieve? Why do you want to remove the items from the ArrayList in the first place?
– Mateusz Mrozewski
Jan 3 at 6:43
The actual ArrayList contains data to be POSTed once internet connection is available. What I'm trying to do was: 1) check if there is internet connection, 2) if there is internet connection, POST data inside ArrayList if any, 3) upon successful submission (
onSuccess
), remove the POSTed item from the ArrayList. Think of it as an ArrayList containing pending data created while offline which are to be submitted once device is conected to the internet.– tyn
Jan 3 at 6:54
The actual ArrayList contains data to be POSTed once internet connection is available. What I'm trying to do was: 1) check if there is internet connection, 2) if there is internet connection, POST data inside ArrayList if any, 3) upon successful submission (
onSuccess
), remove the POSTed item from the ArrayList. Think of it as an ArrayList containing pending data created while offline which are to be submitted once device is conected to the internet.– tyn
Jan 3 at 6:54
1
1
ArrayList does not seem to be a right choince here as it is kept in memory. What if your app gets killed by OS before internet connection is available? Probably chosing SQLite or some other persistent store will be a better option. Then the solution to your problem will depend on the option you will choose.
– Mateusz Mrozewski
Jan 3 at 7:28
ArrayList does not seem to be a right choince here as it is kept in memory. What if your app gets killed by OS before internet connection is available? Probably chosing SQLite or some other persistent store will be a better option. Then the solution to your problem will depend on the option you will choose.
– Mateusz Mrozewski
Jan 3 at 7:28
I'm actually using Shared Preference to store the ArrayList as JSON string. Whenever I need to retrieve/save the data, I need to parse the JSON string into ArrayList. I've been reading on SQLite implementation as well and this gives me more reason to actually implement it. Thank you very much.
– tyn
Jan 3 at 7:33
I'm actually using Shared Preference to store the ArrayList as JSON string. Whenever I need to retrieve/save the data, I need to parse the JSON string into ArrayList. I've been reading on SQLite implementation as well and this gives me more reason to actually implement it. Thank you very much.
– tyn
Jan 3 at 7:33
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54016255%2farraylist-iterator-remove-inside-asynchttpclient-throws-illegalstateexception%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Why you are doing all Async calls inside
while(iterator.hasNext())
?iterator.remove()
insideonSuccess
is causing the problem . Since its an Asynchronous call thats whyiterator.remove()
is throwingIllegalStateException
You should look for a elegant way to do this.– ADM
Jan 3 at 4:20
@ADM I am sorry for my unelegant way as I am still learning android. What i'm thinking is: after successfully sending the arraylist by HTTP POST, I want it to be removed from the arraylist. That's why
remove()
is called insideonSuccess
. What other way can I do it?– tyn
Jan 3 at 6:15