android post json,应用引擎给出 404
android post json, app engine gives 404
我想同时学习更多关于 webbbservices 和 python 的知识。
因此,如果您有想法或解决方案,请像我 5 一样向我解释。:)
所以我想向服务器发送一个字符串并将其存储在数据库(留言簿)中。
我已经设法用网页做到这一点,但现在我想通过 phone 访问和存储一个字符串,这是 python 代码:
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)
这是 android 代码:
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());
}
}
在您的路由 table 中:(r'/chat/(\d+)', Chat),
此行将 url 映射到处理程序。
通过在处理程序的 get 函数中将 (\d+)
映射到 product_id
来处理。
有效 urls
- /chat/1
- /chat/302490205
无效 urls
- /聊天
- /chat/jedi
编辑
因为您 post 需要在处理程序中使用 post 方法
def post(self):
#do stuff
我想同时学习更多关于 webbbservices 和 python 的知识。 因此,如果您有想法或解决方案,请像我 5 一样向我解释。:)
所以我想向服务器发送一个字符串并将其存储在数据库(留言簿)中。 我已经设法用网页做到这一点,但现在我想通过 phone 访问和存储一个字符串,这是 python 代码:
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)
这是 android 代码:
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());
}
}
在您的路由 table 中:(r'/chat/(\d+)', Chat),
此行将 url 映射到处理程序。
通过在处理程序的 get 函数中将 (\d+)
映射到 product_id
来处理。
有效 urls
- /chat/1
- /chat/302490205
无效 urls
- /聊天
- /chat/jedi
编辑
因为您 post 需要在处理程序中使用 post 方法
def post(self):
#do stuff