以下のスクリプトを使用して、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 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)