Android Volley似乎无法连接到Flask本地主机地址以获取从Flask返回字符串的GET方法?

2024-09-28 22:40:18 发布

您现在位置:Python中文网/ 问答频道 /正文

这是Android截击码:

// I already add the uses-permission for INTERNET to manifest
<uses-permission android:name="android.permission.INTERNET" />

// and add volley to gradle depedencies
compile 'com.android.volley:volley:1.0.0'


StringRequest stringRequest; 
RequestQueue mRequestQueue;  

String url = "http://localhost:5000/bacon"; // This is the localhost to FLask

RequestQueue queue = Volley.newRequestQueue(BaconActivity.this);
stringRequest = new StringRequest(
            Request.Method.GET,
            url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Toast.makeText(BaconActivity.this, response, Toast.LENGTH_SHORT).show();
                    redirectLinkToLogin();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    Toast.makeText(BaconActivity.this, "That didn't work!", Toast.LENGTH_SHORT).show();
                }
            }
    );

queue.add(stringRequest);

这是烧瓶代码:

^{pr2}$

当我运行应用程序时,我的截击没有连接到我的烧瓶(我已经在运行应用程序之前运行了烧瓶)。因此,不是检索返回字符串,而是运行到响应.ErrorListener每一次。在

我试图从Flask获取返回字符串到本地主机,并为返回字符串干杯。在


Tags: theto字符串addnewstring烧瓶this
2条回答
String url = "http://localhost:5000/bacon"; // This is the localhost to FLask

实际上,不是。这是运行设备的本地主机。不管是仿真器,还是你的安卓设备。在

您可能还需要烧瓶来运行host='0.0.0.0'

你需要使用你的电脑的实际IP运行烧瓶。我相信Android模拟器使用10.0.2.2

我在Mac电脑上解决了这个问题,方法是转到System Preferences>;Network并记录我的ip。在

然后我用这个ip地址重新启动了我的dev-appserver。在

dev_appserver.py host="192.168.1.109" app.yaml

我在我的JsonObjectRequest声明中也使用了相同的ip地址(作为Volley请求的一部分)。在

现在我可以使用Volley对我的本地Google端点进行GET调用。在

相关问题 更多 >