I am sending a HTTP POST request from android to a server using the script below
URI website = new URI("http://venkygcm.appspot.com");HttpClient client = new DefaultHttpClient();HttpPost request = new HttpPost(website);request.setHeader("Content-Type", "application/json");String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());JSONObject obj = new JSONObject();obj.put("reg_id","Registration ID sent to the server"); obj.put("datetime",currentDateTimeString);StringEntity se = new StringEntity(obj.toString());request.setEntity(se);HttpResponse response = client.execute(request);String out = EntityUtils.toString(response.getEntity());
As I have sent a JSON Object, I must receive a JSON Object in the server. Instead I get a string containing the data of the body. The server is made in Python Google App Engine.
import webapp2class MainPage(webapp2.RequestHandler):def post(self):self.response.out.write(" This is a POST Request \n")req = self.requesta = req.get('body')self.response.out.write(type(a))app = webapp2.WSGIApplication([('/', MainPage)], debug=True)
I tried what AK09 suggested but i still get a string kind of object. What should be my next step?
import webapp2
import jsonclass MainPage(webapp2.RequestHandler):def post(self):self.response.out.write("This is a POST Request \n")req = self.requesta = req.get('body')b = json.dumps(a)self.response.out.write(type(a))self.response.out.write(type(b))app = webapp2.WSGIApplication([('/', MainPage)], debug=True)