Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Python Google应用程序引擎接收字符串而不是JSON对象_Android_Python_Json_Google App Engine_Http Post - Fatal编程技术网

Android Python Google应用程序引擎接收字符串而不是JSON对象

Android Python Google应用程序引擎接收字符串而不是JSON对象,android,python,json,google-app-engine,http-post,Android,Python,Json,Google App Engine,Http Post,我正在使用下面的脚本从android向服务器发送HTTP POST请求 URI website = new URI("http://venkygcm.appspot.com"); HttpClient client = new DefaultHttpClient(); HttpPost request = new HttpPost(website); request.setHeader("Cont

我正在使用下面的脚本从android向服务器发送HTTP POST请求

            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()); 
因为我已经发送了一个JSON对象,所以我必须在服务器中接收一个JSON对象。相反,我得到了一个包含主体数据的字符串。服务器是用Python和Google应用程序引擎制作的

 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)
我尝试了AK09的建议,但我仍然得到了字符串类型的对象。我下一步该怎么做

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)

最后这段代码成功了

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显示为所需的类型列表。

是。这是我第一次做这个。那么,你能告诉我HTTP是如何工作的吗?我应该如何继续我想要实现的。Venkatesh,在服务器上,你必须处理请求并将其解析为Json。看看这个