Reputation: 818
Im trying to learn more about webbbservices and python at the same time. So if you got ideas or solution, explain to me like Im 5. :)
So I want to send a string to the server and just store it in a database (guestbook). I've managed to do this with a webpage but now I want to access and store a string by phone, this is the python code:
import os
import urllib
import json
from google.appengine.ext import ndb
import jinja2
import webapp2
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'],
autoescape=True)
DEFAULT_GUESTBOOK_NAME = 'default_guestbook'
GUESTBOOKS_NAME = 'guestbook'
def guestbook_key(guestbook_name=DEFAULT_GUESTBOOK_NAME):
return ndb.Key('Guestbook', guestbook_name)
class Guestbook(ndb.Model):
identity = ndb.StringProperty(indexed=True
class Chat(webapp2.RequestHandler):
def get(self):
guestbook = Guestbook(parent=guestbook_key(GUESTBOOKS_NAME))
guestbook.identity=self.request.get("content")
guestbook.put()
self.response.headers['Content-Type'] = "text/plain"
self.response.out.write("ok")
application = webapp2.WSGIApplication([
(r'/chat', Chat),
], debug=True)
and this is the android code:
private void sendData(){
try {
JSONObject jsonobj = new JSONObject();
jsonobj.put("content", "asdf1234");
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppostreq = new HttpPost("http://<myappid>.appspot.com/chat/");
StringEntity se = new StringEntity(jsonobj.toString());
se.setContentType("application/json;charset=UTF-8");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));
httppostreq.setEntity(se);
HttpResponse httpresponse = httpclient.execute(httppostreq);
Log.d("Debug", "Response: " + EntityUtils.toString(httpresponse.getEntity()));
} catch (JSONException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e){
Log.d("Debug", "Exception: " + e.getMessage());
}
}
Upvotes: 0
Views: 111
Reputation: 4985
In your routing table : (r'/chat/(\d+)', Chat),
this line maps the url to the handler.
which is handled by mapping the (\d+)
to product_id
in the get function of the handler.
valid urls
invalid urls
edit
since your posting your need a post method in your handler
def post(self):
#do stuff
Upvotes: 1