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.
我在网上看到很少有关于使用Tornado创建Web服务器以处理来自Android手机的GET / POST请求的信息。我对Android和Web服务器dev都比较陌生,但我对Tornado有一些经验,所以我想我会使用它,尽管似乎缺乏使用这两种技术的教程。
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.
我正在使用HttpURLConnection类来创建与Web服务器的连接,它与我使用的教程一起工作,但我现在正尝试将我的Android应用程序连接到我的计算机上的Tornado Web服务器以简单地获取“Hello world” “当我点击我的Android应用程序上的按钮时,从服务器。我没有提供教程的http.php / get URL(经过测试和工作),而是想尝试使用计算机的IP连接我的服务器,所以我用http:// xxxx:8888 /替换了URL,我的IPv4地址,但不起作用。
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.
如有必要,我可以提供更多我的安卓代码。谢谢你的帮助,我完全无法解决这个问题。
1 个解决方案
#1
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.
是的,你可以处理来自android的GET / POST调用。您也可以从服务器PHP页面获取响应到Android应用程序作为数组或字符串。
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
服务器上的register.php文件
<?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.
您应该添加google volley.jar作为您的jar依赖项。
#1
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.
是的,你可以处理来自android的GET / POST调用。您也可以从服务器PHP页面获取响应到Android应用程序作为数组或字符串。
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
服务器上的register.php文件
<?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.
您应该添加google volley.jar作为您的jar依赖项。