I am trying to remove duplicates from json string using the answer from Remove duplicates from a Json String in Java? question,but it is not working.Here is my code
inquire.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//location = loc.getText().toString();
StringRequest request = new StringRequest(Request.Method.POST, urlRec, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
String responseString = response.toString();
JSONObject obj = new JSONObjectIgnoreDuplicates(responseString);
JSONArray recJsonArray = obj.getJSONArray("search");
// JSONArray recJsonArray = new JSONArray(responseString);
List<Recipient> recList = new ArrayList<>();
for (int i = 0; i < recJsonArray.length(); i++) {
JSONObject recJsonObj = (JSONObject) recJsonArray.get(i);
Recipient rec = new Recipient();
rec.setname(recJsonObj.getString("name"));
rec.setLocation(recJsonObj.getString("location"));
rec.setcontact(recJsonObj.getString("contact"));
recList.add(rec);
}
Intent myintent = new Intent(Search_inq.this, Reclist.class);
myintent.putExtra("recList", (Serializable) recList);
startActivity(myintent);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Error", error.getMessage());
Toast.makeText(getApplication(), "Error occurred", Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parameters = new HashMap<>();
parameters.put("location", location);
return parameters;
}
};
queue.add(request);
}
});
Here,the table on which i conducted query is called SEARCH.Please help if you can.
edit
here is the code for the class JSONObjectIgnoreDuplicates
public class JSONObjectIgnoreDuplicates extends JSONObject {
public JSONObjectIgnoreDuplicates(String json) throws JSONException {
super(json);
}
public JSONObject putOnce(String key, Object value) throws JSONException {
Object storedValue;
if (key != null && value != null) {
if ((storedValue = this.opt(key)) != null ) {
if(!storedValue.equals(value)) //Only through Exception for different values with same key
Log.d("not accepted","n");
else
return this;
}
this.put(key, value);
}
return this;
}
}
edit-
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reclist);
Intent i = getIntent();
// List<Recipient> recList = (List<Recipient>) i.getParcelableArrayListExtra("recList");
ArrayList<Recipient> recList = getIntent().getParcelableArrayListExtra("recList");
list = (ListView) findViewById(R.id.list1);
onLoaded(recList);
}
public void onLoaded(List<Recipient> recList) {
for (Recipient rec : recList) {
HashMap<String, String> map = new HashMap<>();
map.put("name", rec.getUserName());
map.put("location", rec.getLocation());
map.put("contact", rec.getContact());
recplist.add(map);
}
loadListView();
}
private void loadListView() {
ListAdapter adapter = new SimpleAdapter(Reclist.this, recplist, R.layout.list_item,
new String[]{"name", "location", "contact"},
new int[]{R.id.textView3, R.id.textView4, R.id.textView6});
list.setAdapter(adapter);
}