I have the Volley method within my android app to access and create an event entry in the rails MySQL database:
JsonObjectRequest createEventGetID = new JsonObjectRequest(Request.Method.POST, "http://" + Global.getFeastOnline() + "/createEvent",
new JSONObject(jsonParams),
new Response.Listener<JSONObject>() {
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("user_id", settings.getString("userID","0"));
params.put("campaign_id", "-1");
params.put("location_id", "-1");
params.put("event_active", event.getAlarmActive().toString());
params.put("event_removed", "0");
params.put("event_time", event.getAlarmTimeString());
params.put("event_days", Base64.encodeToString(buff, Base64.DEFAULT));
params.put("event_tone", event.getAlarmTonePath());
params.put("event_vibrate", event.getVibrate().toString());
params.put("event_name", event.getAlarmName());
params.put("event_date", event.getAlarmDateString());
return params;
}
@Override
public void onResponse(JSONObject response) {
// Parse the JSON:
try {
// Returning JSON, tag:
JSONArray newEventID = response.getJSONArray("new_event_server_id");
Toast.makeText(getApplicationContext(), newEventID.toString(), Toast.LENGTH_LONG).show();
// PARSE THE REST
//Log.v("New Event ID", "The new event id is " + userId);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// error
Log.d("Create Events Error", error.toString());
}
}
);
requestQueue.add(createEventGetID);
Volley has no problem sending as they are all strings in the POST request. The createEvent method in rails is:
def createEvent
createEvent = "INSERT INTO events(
event_creator_id
, campaign_id
, location_id
, event_active
, event_removed
, event_time
, event_days
, event_tone
, event_vibrate
, event_name
, event_date
, created_at
, updated_at
) VALUES ("
+ params[:user_id].to_s + ","
+ params[:campaign_id].to_s + ","
+ params[:location_id].to_s + ","
+ params[:event_active].to_s + ","
+ params[:event_removed].to_s + ","
+ params[:event_time].to_s + ","
+ params[:event_days].to_s + ","
+ params[:event_tone].to_s + ","
+ params[:event_vibrate].to_s + ","
+ params[:event_name].to_s + ","
+ params[:event_date].to_s + ","
+ "NOW(),NOW()"
+ ")"
ActiveRecord::Base.connection.execute(createEvent)
end
and route as:
match '/createEvent', to: "requests#createEvent", via: 'post'
When testing the volley action, the rails command lines throws error as:
NoMethodError (undefined method `+@' for "":String):
app/controllers/requests_controller.rb:522:in `createEvent'
on line: + params[:user_id].to_s + ","
of the createEvent
method.
I have tried everything including removing all inserted values form the createEvent
method aside from a hard-coded "0", yet the error persists.
How can this be alleviated?
Whoa, that is not a good design of controller, not to say your code is not secure (waiting for an SQL injection to happen).
Just use ActiveRecord, create Event model (put this into app/models/event.rb):
class Event < ActiveRecord::Base
end
Then edit your controller:
def createEvent
Event.create!(event_creator_id: params[:user_id], campaign_id: params[:campaign_id], ...) # add all your parameters
head :no_content
end
On your initial question: you have to put '+' signs on the same line with previous string, so you will have something like:
"...) VALUES (" + params[:user_id].to_s + "," + # plus is at the end of line, not the beginning
params[:campaign_id].to_s + "," +
# ... etc.