Reputation: 6080
I'm having some trouble parsing some JSON returned by my rails server in a basic survey Android app.
Essentially I want to first POST a response. Then save the ID it gets in response.
I'm getting the ID returned, however it seems to be null
when I log it out while the other "survey_id"
and "appuser_id"
paramaters do get set.
Because my Controller is set with the params like this:
def response_params
params.require(:response).permit(:survey_id, :completed, :appuser_id)
end
It expects my JSON post to be in the format of '{"response": {"survey_id":"1", "appuser_id":"1"}}'
Thus why I create a ResponseDetail
to house those details in each response.
Here's the JSON I'm receiving back from my Rails server after the successful POST:
{"id":162,"survey_id":1,"completed":false,"created_at":"2015-03-17T13:31:10.403Z","updated_at":"2015-03-17T13:31:10.403Z"}
How I'm Posting it:
public static Response postResponse(Response r) {
Gson g = new Gson();
String json = g.toJson(r);
String responsesUrl = Uri.parse(RESPONSE_ENDPOINT).buildUpon().build().toString();
Response resp = null;
try {
HttpResponse response = makeRequest(responsesUrl, json);
String jsonResponse = EntityUtils.toString(response.getEntity());
int status = response.getStatusLine().getStatusCode();
if (status == 201) {
JSONObject object = new JSONObject(jsonResponse);
Log.d(TAG, "Object is: " + object.toString());
resp = new Response(object);
}
} catch (Exception e) {
e.printStackTrace();
}
return resp;
}
Here's how I parse it:
public class Response {
private ResponseDetail response;
public Response(ResponseDetail responseDetail) {
response = responseDetail;
}
public Response(JSONObject json) throws JSONException {
String surveyId = json.getInt("survey_id") + "";
String appuserId = json.getInt("appuser_id") + "";
String id = json.getInt("id") + "";
ResponseDetail detail = new ResponseDetail(surveyId, appuserId, id);
this.response = detail;
}
//getters/setters removed
}
class ResponseDetail {
private String id;
private String survey_id;
private boolean completed;
private String appuser_id;
public ResponseDetail(String surveyId, String appuserId) {
super();
this.survey_id = surveyId;
this.appuser_id = appuserId;
}
public ResponseDetail(String surveyId, String appuserId, String id) {
super();
this.id = id;
this.survey_id = surveyId;
this.appuser_id = appuserId;
}
//getters and setters removed
}
Finally the Async where I post it. Where it logs out the appuser_id and survey_id appropriately but shows the ID as null.
private class PostResponse extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params) {
try {
ResponseDetail detail = new ResponseDetail("1", "344");
Response response = new Response(detail);
new SurveyConnection().postResponse(response);
Log.d("SurveyConnection", "Appuser: " + response.getResponse().getAppuser_id());
Log.d("SurveyConnection", "Survey: " + response.getResponse().getSurvey_id());
Log.d("SurveyConnection", "ID: " + response.getResponse().getId());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
Toast.makeText(getActivity(), "It worked!", Toast.LENGTH_SHORT).show();
}
}
Log:
03-17 08:40:52.225 3217-3233/getrewards.example.com.getrewards D/SurveyConnection﹕ Appuser: 344
03-17 08:40:52.225 3217-3233/getrewards.example.com.getrewards D/SurveyConnection﹕ Survey: 1
03-17 08:40:52.225 3217-3233/getrewards.example.com.getrewards D/SurveyConnection﹕ ID: null
Any ideas what I need to do to get the ID to be set? I tried both json.getInt("id")
and json.getString("id")
but they both seem to return null
despite the fact that I see ID
in the returned JSON.
Upvotes: 0
Views: 263
Reputation: 2038
you´ve got ResponseDetail detail = new ResponseDetail("1", "344");
and id goes null in there...
you see? it´s hardcoded, nothing to do with your networking
you should get the return value of this line new SurveyConnection().postResponse(response);
(By the way it´s crazy to read your code, everything is called postResponse
or Response
, even the request is called response! ^g^
Upvotes: 1