-1
我從Android的使用下面Python的谷歌應用程序引擎接收JSON對象代替字符串
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());
腳本發送一個HTTP POST請求到服務器,當我打發一個JSON對象,我必須接受服務器中的JSON對象。相反,我得到一個包含正文數據的字符串。該服務器是在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)
我嘗試了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)
是的。這是我第一次爲此工作。那麼你能告訴我HTTP是如何工作的嗎?我應該如何繼續我想要達到的目標。 – VenkateshShukla
Venkatesh,在服務器上您必須處理請求並將其解析爲Json。看看這個http://stackoverflow.com/questions/1171584/how-can-i-parse-json-in-google-app-engine?rq=1 –