1

I've seen very little information online for creating a web server with Tornado to handle GET/POST requests from an android phone. I'm relatively new to both Android and web server dev but I have some experience with Tornado so I thought I would use that, although there seems to be a huge lack of tutorials for using these two technologies.

import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
   def get(self):
      self.write("Hello, world")

def main():
   return tornado.web.Application([
      (r"/", MainHandler),
   ])

if __name__ == "__main__":
   app = main()
   app.listen(8888)
   tornado.ioloop.IOLoop.current().start()

I'm using the HttpURLConnection class to create a connection with the web server, which has worked with a tutorial I used, but I'm now trying to connect my android app to my Tornado web server on my computer to simply get "Hello world" from the server when I click a button on my android app. Instead of providing the tutorial's http.php/get URL (which is tested and works), I want to try to use my computer's IP to connect with my server, so I replaced the URL with http://x.x.x.x:8888/, my IPv4 address, but that doesn't work.

GetBtn.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View V) {
      new MakeNetworkCall().execute("http://androidpala.com/" +
         "tutorial/http.php?get=1", "Get");
   }
});

I can provide more of my android code if necessary. Thanks for the help, I can't figure this out at all.

Seany242
  • 373
  • 7
  • 20
  • This is not particularly related to Tornado. It is rather about accessing your localhost from an external device. There seem to be some answers on [this question](https://stackoverflow.com/q/4779963/1925257). – xyres Feb 23 '18 at 16:51
  • I'm running Ubuntu OS and the way I do it is I turn on my **[computer's hotspot](https://help.ubuntu.com/stable/ubuntu-help/net-wireless-adhoc.html)** then connect my phone to that hotspot. Then I run Tornado or any other server for that matter. Finally I visit the IP address of my computer in from phone's browser. It works for me. – xyres Feb 23 '18 at 17:01
  • @xyres thank you for that link, but I think that only answers one part of my question. If I want to display the webpage that my server is supporting, I can use that answer to go to http://x.x.x.x:8888/ to see the webpage handled by the r"/" URL. I'm more concerned with handling direct GET/POST requests with the server as I'd like to connect my Android to a database handled by the server. Thanks! – Seany242 Feb 24 '18 at 11:15
  • Tornado allows you to handle GET and POST methods via `get(self)` and `post(self)` methods. – xyres Feb 24 '18 at 11:33

1 Answers1

0

Yes, you can handle GET / POST calls from android. Also you can get response from server PHP page into android app as arrays or string.


StringRequest stringRequest = new StringRequest(Request.Method.POST, "https://www.socialmatrimony.in/insta_promo/register.php",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Toast.makeText(getActivity(),response,Toast.LENGTH_LONG).show();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getActivity(),error.toString(),Toast.LENGTH_LONG).show();
                }
            }){
        @Override
        protected Map<String,String> getParams(){
            Map<String,String> params = new HashMap<String, String>();
            params.put("NAME",NameField.getText().toString());
            params.put("EMAIL",EmailField.getText().toString());
            params.put("PHONENUMBER", NumberField.getText().toString());
            params.put("IMEI", MainActivity.deviceid);
            return params;
        }

    };

    RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
    requestQueue.add(stringRequest);

register.php file on server


<?php

if($_SERVER['REQUEST_METHOD']=='POST')
{
$username = $_POST['user'];
$password = $_POST['pass'];
$value = $_POST['val'];

// do operations here

echo "success@".$username.$password.$value;

}
else
{
echo "error";

}

You should add google volley.jar as your jar dependency.

  • Let me know if you want assistance for jar dependency –  Feb 23 '18 at 14:22
  • I understand how to connect to a website online (https://www.socialmatrimony.in/insta_promo/register.php) but my question is about connecting to a server my computer. How can I accomplish that? – Seany242 Feb 23 '18 at 14:37
  • You should look for "How to mount remote file system" –  Feb 23 '18 at 16:30