Python的谷歌应用程序引擎的JSON对象代替接收字符串 [英] Python Google App Engine Receiving a string in stead of JSON object

查看:126
本文介绍了Python的谷歌应用程序引擎的JSON对象代替接收字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面

脚本发送从一个android的HTTP POST请求到服务器

  URI网站=新的URI(http://venkygcm.appspot.com);            HttpClient的客户端=新DefaultHttpClient();
            HttpPost要求=新HttpPost(网站);            request.setHeader(内容类型,应用/ JSON);            字符串currentDateTimeString = DateFormat.getDateTimeInstance()格式(新的Date());            JSONObject的OBJ =新的JSONObject();
            obj.put(REG_ID,注册ID发送到服务器);
            obj.put(日期时间,currentDateTimeString);            StringEntity本身=新StringEntity(obj.toString());
            request.setEntity(SE);
            HTT presponse响应= client.execute(请求);            串出= EntityUtils.toString(response.getEntity());

正如我已经发出了JSON对象,我必须接受服务器的JSON对象。相反,我得到含有人体的数据串。服务器在Python谷歌应用程序引擎的。

 进口webapp2的一流的MainPage(webapp2.RequestHandler):
    DEF后(个体经营):
        self.response.out.write(这是一个POST请求\\ n)
        REQ = self.request获取
        A = req.get(身体)
        self.response.out.write(式(a))的应用= webapp2.WSGIApplication([('/',的MainPage),调试=真)

我想提出什么AK09,但我仍然得到一个字符串类型的对象。应该是什么我的下一个步骤?

 进口webapp2的
进口JSON一流的MainPage(webapp2.RequestHandler):
    DEF后(个体经营):
        self.response.out.write(这是一个POST请求\\ n)
        REQ = self.request获取
        A = req.get(身体)
        B = json.dumps(一)        self.response.out.write(式(a))的
        self.response.out.write(类型(b))的应用= webapp2.WSGIApplication([('/',的MainPage),调试=真)


解决方案

最后这个code工作

 进口webapp2的
进口JSON一流的MainPage(webapp2.RequestHandler):
    DEF后(个体经营):
        self.response.out.write(这是一个POST请求\\ n)
        REQ = self.request获取
        A = req.body
        B = json.loads(一)        self.response.out.write(二)
        self.response.out.write(二['REG_ID'])
        self.response.out.write(B ['日期时间'])
        self.response.out.write(类型(b))的应用= webapp2.WSGIApplication([('/',的MainPage),调试=真)

乙出来的要求是类型列表中。

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 webapp2

class MainPage(webapp2.RequestHandler):
    def post(self):
        self.response.out.write(" This is a POST Request \n")
        req = self.request
        a = 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 json

class MainPage(webapp2.RequestHandler):
    def post(self):
        self.response.out.write("This is a POST Request \n")
        req = self.request
        a = 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)

解决方案

Finally this code worked

import webapp2
import json

class MainPage(webapp2.RequestHandler):
    def post(self):
        self.response.out.write("This is a POST Request \n")
        req = self.request
        a = req.body
        b = json.loads(a)

        self.response.out.write(b)
        self.response.out.write(b['reg_id'])
        self.response.out.write(b['datetime'])
        self.response.out.write(type(b))

app = webapp2.WSGIApplication([('/', MainPage)], debug=True)

b comes out to be of the type List as is required.

这篇关于Python的谷歌应用程序引擎的JSON对象代替接收字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆