I'm a beginner at Android Studio and I'm using Ajax to send data to my database server. I have two types of HashMap parameters that I need to query. One is for all the String values in my form and the other is for the image file but I can't seem to send them in a single call to the new Aquery.ajax(//parameters) method. All the variations of this method limit the use of only one Map type.
I tried putting the ParamFile parameter in a different query but it still doesn't work. Though it could be because I'm not completely sure what an "Object Handler" means here in the other method definition.
Here is the code for my method:
private void AddNewEquipment()
{
try {
//Two types of HashMap files
HashMap<String, String> param = new HashMap<>();
HashMap<String, Bitmap> paramFile = new HashMap<>();
param.put("listing_title", ETitle.getText().toString());
param.put("listing_type", String.valueOf(IntEquipmentType));
param.put("listing_desc", EDesc.getText().toString());
param.put("listing_rate", ERate.getText().toString());
param.put("listing_mode", Mode);
paramFile.put("listing_img_file", bitmap); //this never gets queried
param.put("listing_date_from-x", AvailableFromDT);
param.put("listing_date_to-x", AvailableToDT);
param.put("listing_sell_mode", SellMode);
param.put("listing_display_mode", String.valueOf(IntAdType));
param.put("listing_status", String.valueOf(IntAdStatus));
AQuery aq = new AQuery(this);
aq.ajax(BASE_URL, param, JSONObject.class, new AjaxCallback<JSONObject>() {
@Override
public void callback(String url, JSONObject json, AjaxStatus status) {
super.callback(url, json, status);
Log.v("CALLBACK RECEIVED", String.valueOf(json) + status);
try {
if (json != null) {
JSONObject h = json.getJSONObject("success");
Log.v("SUCCESS", "DONE");
Toast.makeText(AddEquipment.this, "New Equipment Added", Toast.LENGTH_LONG).show();
exitEquipmentForm(); //method that opens the main activity again
} else {
Log.v("ERROR", "ERROR");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.method(AQuery.METHOD_POST).header("Authorization", "Bearer "+SessionHandler.getKeyToken()));
} catch (Exception e) {
e.printStackTrace();
}
}
I keep getting null JSON objects, although that is because not all parameters get queried <---The part where I need help with.
I'm happy to provide more snippets of my code. Please let me know of any alternatives as well.
If you are initializing HashMap with fixed data type then you can not store any other values for that other then specified datatype. If you want to store any data as a value then you should use Object data type.
Like
HashMap<String, Object> param = new HashMap<>();
Then after you can add any value (Bitmap or String) as a Map value.
You can store the value like,
HashMap<String, Object> param = new HashMap<>();
param.put("listing_title", ETitle.getText().toString()); // String values
// add Other String parameters here..
param.put("listing_img_file", bitmap); // Your Image Bitmap file
My suggestion is replace your HashMap<String, Object>
with LinkedHashMap<String, Object>
. HashMap
doesn't maintain any order while LinkedHashMap
maintains insertion order of elements.