Reputation: 299
I'm using an android app to submit to a Rails app with a Mongo backend hosted on Heroku. I am able to use the form online to submit a new item into the mongo database, but when I try to submit JSON from an android app I get:
{"error":"Unprocessable Entity","status":"422"}
I think this may have to do with the order of JSON when I submit as it seems to have gotten mixed up in order which I realized is normal according to a past SO question. But, the solution on that question did not work for me.
Here is the construction of the JSONObject in my Android app:
JSONObject jsonObject = new JSONObject();
jsonObject.put("ph", 2);
jsonObject.put("chlorine", 2.0);
jsonObject.put("magnified_Link", url);//URLEncoder.encode(encodedImage, "UTF-8"));
jsonObject.put("taste", "yucky");
jsonObject.put("odor", "smelly");
jsonObject.put("temperature", "77.0");
jsonObject.put("mercury", 234);
jsonObject.put("hardness", 9.0);
jsonObject.put("lat", latitude);
jsonObject.put("long", longitude);
String json = jsonObject.toString(); // Output to string
Log.d(TAG, json);
StringEntity se = new StringEntity(json);
// put json string into server
httpPost.setEntity(se);
//httpPost.setHeader("Authorization", "Client-ID " +API_KEY);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
This is how the JSON appears in my android app when I log it before submission:
{"chlorine":2,"odor":"smelly","magnified_Link":"http:\/\/i.imgur.com\/JnbuUwy.jpg","long":0,"hardness":9,"ph":2,"taste":"yucky","lat":0,"mercury":234,"temperature":"77.0"}
This is how my JSON page looks like on my rails app with one successful submission into it through the online rails app:
[{"id":{"$oid":"558766f26633380003000000"},"ph":1.0,"chlorine":2.0,"magnified_Link":"3","taste":"4","odor":"5","temperature":6.0,"mercury":7.0,"hardness":8.0,"lat":40.7127,"long":-74.0059,"url":"https://distributed-health.herokuapp.com/distributed_healths/558766f26633380003000000.json"}]
Finally here is the model for a "DistributedHealth" (my object) in my rails app:
class DistributedHealth
include Mongoid::Document
field :ph, type: Float
field :chlorine, type: Float
field :magnified_Link, type: String
field :taste, type: String
field :odor, type: String
field :temperature, type: Float
field :mercury, type: Float
field :hardness, type: Float
field :lat, type: Float
field :long, type: Float
end
I can include any other relevant code and will make edits to this post as I continue trying to solve this issue.
Thank you,
Clayton.
EDIT: I have the error message from my heroku server
2015-06-23T21:29:34.382730+00:00 heroku[router]: at=info method=POST path="/distributed_healths.json" host=distributed-health.herokuapp.com request_id=2dcde4a4-3dcc-4381-98ba-6ca399400a6d fwd="128.54.58.245" dyno=web.1 connect=3ms service=14ms status=422 bytes=328
Upvotes: 2
Views: 1936
Reputation: 299
The issue ended up being a Rails side one and didn't have to do with the order of the JSON being submitted. I turned out the need for a CSRF token using the line right after class DistributedHealthsController < ApplicationController
in my Controller:
skip_before_action :verify_authenticity_token
This isn't recommended as it is a work around security, but if you want to exclude certain methods this post will show you how.
More discussion on the topic can be found here and some documentation on the topic can be found here.
Even though I am now able to do a POST to my rails app I am still getting a Error 500 in my Android Studio log that looks like this:
{"error":"Internal Server Error","status":"500"}
It doesn't seem to be hindering functionality right now so I am not going to lose any sleep over it.
Upvotes: 2