Java 如何将来自服务器的最新响应放入arraylist?
在我的数据库中更新我的联系人时,Java 如何将来自服务器的最新响应放入arraylist?,java,arraylist,Java,Arraylist,在我的数据库中更新我的联系人时,响应正确显示,但arraylist不正确-添加联系人时,arraylist正常,但删除的旧联系人在应该消失时仍然存在。你能帮忙吗 private void CheckifUserisContact() { StringRequest stringRequest = new StringRequest(Request.Method.POST, CHECKPHONENUMBER_URL, new Response.Listene
响应
正确显示,但arraylist
不正确-添加联系人时,arraylist
正常,但删除的旧联系人在应该消失时仍然存在。你能帮忙吗
private void CheckifUserisContact() {
StringRequest stringRequest = new StringRequest(Request.Method.POST, CHECKPHONENUMBER_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println("The contacts are :" + response);
String MatchingContactsAsString = response.toString();
System.out.println("The contacts are :" + MatchingContactsAsString);
try {
JSONArray Object = new JSONArray(MatchingContactsAsString);
//for every object in the Array
for (int x = 0; x < Object.length(); x++) {
final JSONObject obj = Object.getJSONObject(x);
MatchingContactsAsArrayList.add(obj.getString("usernameMatch"));
//to stop duplicates sometimes happening....
HashSet<String> hashSet = new HashSet<String>();
hashSet.addAll(MatchingContactsAsArrayList);
MatchingContactsAsArrayList.clear();
MatchingContactsAsArrayList.addAll(hashSet);
}
System.out.println("The contacts are :" + MatchingContactsAsArrayList);
这是正确的
第二行,System.out.println(“联系人为:“+matchingcontactsassstring”)代码>,显示:[{“usernameMatch”:“+123456”},{“usernameMatch”:“+789012”}]
这是正确的
第三行,System.out.println(“联系人为:“+matchingcontactsasaraylist”)代码>,显示:[+123456,+55555,+789012]
这是错误的。我删除了,+55555
,但它仍然显示出来。试试这个:
System.out.println("The contacts are: " + response);
String MatchingContactsAsString = response.toString();
System.out.println("The contacts are: " + MatchingContactsAsString);
try {
JSONArray Object = new JSONArray(MatchingContactsAsString);
//for every object in the Array
for (int x = 0; x < Object.length(); x++) {
final JSONObject obj = Object.getJSONObject(x);
MatchingContactsAsArrayList.add(obj.getString("usernameMatch"));
}
HashSet<String> hashSet = new HashSet <String> ();
hashSet.addAll(MatchingContactsAsArrayList);
MatchingContactsAsArrayList.clear();
MatchingContactsAsArrayList.addAll(hashSet);
MatchingContactsAsArrayList.remove("+55555");
System.out.println("The contacts are: " + MatchingContactsAsArrayList);
System.out.println(“联系人为:“+响应”);
字符串匹配ContactsString=response.toString();
System.out.println(“联系人为:“+匹配联系人字符串”);
试一试{
JSONArray对象=新的JSONArray(匹配ContactsAsString);
//对于数组中的每个对象
对于(int x=0;x
一旦收到响应,您就可以清除其内容列表,因为响应值将重新填充该列表。无需删除重复项
private void CheckifUserisContact() {
StringRequest stringRequest = new StringRequest(Request.Method.POST, CHECKPHONENUMBER_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//We have a response so clear the list
MatchingContactsAsArrayList.clear();
String MatchingContactsAsString = response.toString();
try {
JSONArray Object = new JSONArray(MatchingContactsAsString);
//for every object in the Array
for (int x = 0; x < Object.length(); x++) {
final JSONObject obj = Object.getJSONObject(x);
final String userMatch = obj.getString("usernameMatch");
if (!MatchingContactsAsArrayList.contains(userMatch) {
MatchingContactsAsArrayList.add(userMatch);
}
}
System.out.println("The contacts are :" + MatchingContactsAsArrayList);
private void CheckifUserisContact(){
StringRequest StringRequest=新建StringRequest(Request.Method.POST,CHECKPHONENUMBER\u URL,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
//我们有一个明确的答复
匹配ContactsArrayList.clear();
字符串匹配ContactsString=response.toString();
试一试{
JSONArray对象=新的JSONArray(匹配ContactsAsString);
//对于数组中的每个对象
对于(int x=0;x
您确定最后一次打印在onResponse
方法中吗?否则它可能会在回调方法执行之前执行。由于方法不完整,很难看到最后到底发生了什么,您在哪里删除+55555
?@JoakimDanielson是的,肯定在onResponse中。@devgianlu MatchingContactsasaraylist.clear();应该这样做。是的,如果55555在ArrayList中,它将被移动到集合中,然后返回ArrayList。在响应之前清除列表我认为我们可以安全地假设将来会有其他元素被删除的情况,然后此解决方案将不会非常有效helpful@JoakimDanielson这是实现他所希望的目标的唯一途径devgianlu的要求。我同意这没有意义。不,这不是唯一的方法,硬编码这样的值真的很糟糕。你没有检查重复项。@devgianlu我删除了它,因为列表现在首先被清除。你是说你已经存储了重复项并在响应中获得了重复项吗?这就是我理解的。@devgi安陆我已更新了我的答案,并包含了一份重复检查
private void CheckifUserisContact() {
StringRequest stringRequest = new StringRequest(Request.Method.POST, CHECKPHONENUMBER_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//We have a response so clear the list
MatchingContactsAsArrayList.clear();
String MatchingContactsAsString = response.toString();
try {
JSONArray Object = new JSONArray(MatchingContactsAsString);
//for every object in the Array
for (int x = 0; x < Object.length(); x++) {
final JSONObject obj = Object.getJSONObject(x);
final String userMatch = obj.getString("usernameMatch");
if (!MatchingContactsAsArrayList.contains(userMatch) {
MatchingContactsAsArrayList.add(userMatch);
}
}
System.out.println("The contacts are :" + MatchingContactsAsArrayList);