android post json,app引擎给出404

时间:2015-03-19 21:00:20

标签: android python json http-status-code-404

我试图同时了解有关webbbservices和python的更多信息。 所以,如果你有想法或解决方案,请向我解释,如我5.)

所以我想将一个字符串发送到服务器并将其存储在数据库(留言簿)中。 我已经设法用一个网页做了这个,但现在我想通过电话访问和存储一个字符串,这是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());
        }


    }

1 个答案:

答案 0 :(得分:1)

在您的路由表中:(r'/chat/(\d+)', Chat),此行将网址映射到处理程序。

通过将(\d+)映射到处理程序的get函数中的product_id来处理。

有效网址

  • /聊天/ 1
  • /聊天/ 302490205

无效网址

  • /聊天
  • /聊天/绝

修改

因为您需要在处理程序中发布post方法

def post(self):
    #do stuff