1

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. I'm new to 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.

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:

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:

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

***Server side Java (working)

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) {
        }
    } 
3
  • What's a summary of your non-working Python code vs your working Java code? Hard to help, since it's at least as easy in Python as in Java to accept HTTP POST requests, without seeing what bugs you may have inserted in your attempted implementation! Commented Feb 25, 2010 at 5:45
  • What error or failure are you getting? Commented Feb 25, 2010 at 20:28
  • Nothing happens server side, no errors. Client side runs with no exceptions. I'm thinking I'll just stick with the java server side for data upload and use python to do everything for the web. Commented Feb 26, 2010 at 3:02

1 Answer 1

4

I know it's been a long time. But I did solve this so I'll post the solution.

Android code:

        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:

def upload(request):
    if request.method == 'POST':
        deviceID = measurement.deviceID = str(request.POST['deviceID'])
        return HttpResponse('Success!')
    else:
        return HttpResponse('Invalid Data')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.