javascript - Android Post to API Doesn't Work, Ajax Post Works OK. Required String parameter 'firstname' is not present" -
i have spring framework api setup receive get/post request works fine on web when trying post android, "required string parameter 'firstname' not present".
i've tried post using different iterations continually same error.
java spring framework
@requestmapping(value = "/adduser", method = requestmethod.post) public string adduser(@requestparam(value="firstname") string firstname, @requestparam(value="lastname") string lastname, @requestparam(value="username") string username, @requestparam(value="password") string password) throws exception { string success = add.adduser(firstname, lastname, username, password); return tojson(success); }
javascript version
var data = { firstname: firstname, lastname: lastname, username : username, password : password }; $.ajax({ type: 'post', url: "<url removed>/import/adduser", success: function(data) { callbacksuccess(json.stringify(data)); }, error: function(data) { callbackfail(data); }, data: data, datatype: "json" });
android
string url = "<url removed>/import/adduser"; inputstream inputstream = null; string result = ""; try { // 1. create httpclient httpclient httpclient = new defaulthttpclient(); // 2. make post request given url httppost httppost = new httppost(url); string json = ""; // 3. build jsonobject jsonobject jsonobject = new jsonobject(); jsonobject.accumulate("firstname", userfirstname); jsonobject.accumulate("lastname", userlastname); jsonobject.accumulate("username", emailstr); jsonobject.accumulate("password", passwordstr); // 4. convert jsonobject json string json = jsonobject.tostring(); // 5. set json stringentity stringentity se = new stringentity(json); // 6. set httppost entity httppost.setentity(se); log.i("sending", json); // 7. set headers inform server type of content httppost.setheader("accept", "application/json"); httppost.setheader("content-type", "application/json"); // 8. execute post request given url httpresponse httpresponse = httpclient.execute(httppost); // 9. receive response inputstream inputstream = httpresponse.getentity().getcontent(); // 10. convert inputstream string if(inputstream != null) result = convertinputstreamtostring(inputstream); else result = "did not work!"; log.i("response", result);
any thoughts or suggestions?
update have since modified spring framework code parameters not required , not receiving values. null variables. there's issue post rather server side.
content-type should application/x-www-form-urlencoded
. see so helped me once
Comments
Post a Comment