简体   繁体   English

Android应用程序通过发布将数据上传到python服务器

[英]Android app uploading data to a python server via post

I have successfully implemented this from android to a java httpservlet on google app engine, but I'd like to use python instead for the server side. 我已经成功地从Android到Google App Engine上的Java httpservlet实现了此功能,但我想在服务器端使用python代替。 I'm new to python. 我是python的新手。 Has anyone done this? 有人这样做吗? I have the guestbook example up and running, but I can't seem to send posts from my android app to the server. 我已经建立并运行了留言簿示例,但似乎无法将帖子从我的android应用发送到服务器。

I'd also like to issue a string response back to the client like "success". 我还想像“成功”一样向客户端发出字符串响应。

A guiding hand would be much appreciated. 指导手将不胜感激。

Thanks 谢谢

***Client side java: ***客户端Java:

URL url = new URL(Const.SERVER_NAME);
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(
    connection.getOutputStream()
);
out.write("content=12345");
out.close();

***Server side Python: ***服务器端Python:

class Upload(webapp.RequestHandler):
    def post(self):
        greeting.content = self.request.get('content') 
        greeting.put()

***Server side Java (working) ***服务器端Java(有效)

public void doPost(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException
{

    try {
        String instring = request.getParameter("content")

        // set the response code and write the response data
        response.setStatus(HttpServletResponse.SC_OK);
        OutputStreamWriter writer = new OutputStreamWriter(response.getOutputStream());

        writer.write("Success");
        writer.flush();
        writer.close();
    } catch (IOException e) {
        try{
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            response.getWriter().print(e.getMessage());
            response.getWriter().close();
        } catch (IOException ioe) {
        }
    } 

I know it's been a long time. 我知道已经很久了。 But I did solve this so I'll post the solution. 但是我确实解决了这个问题,所以我将发布解决方案。

Android code: Android代码:

        url = new URL(SERVER_URL);
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);

        OutputStreamWriter out = new OutputStreamWriter(
                                  connection.getOutputStream());

        String post_string;
        post_string = "deviceID="+tm.getDeviceId().toString();

        // send post string to server
        out.write(post_string);
        out.close();

        //grab a return string from server
        BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                    connection.getInputStream()));

        Toast.makeText(context, in.readLine(), Toast.LENGTH_SHORT).show();

And here's the python server side, using Django with GAE: 这是将Django与GAE结合使用的python服务器端:

def upload(request):
    if request.method == 'POST':
        deviceID = measurement.deviceID = str(request.POST['deviceID'])
        return HttpResponse('Success!')
    else:
        return HttpResponse('Invalid Data')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM