// and add volley to gradle depedenciescompile 'com.android.volle">
这是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);
这是烧瓶代码:
from flask import Flask, request
app = Flask(__name__)
@app.route('/bacon', methods=['GET', 'POST'])
def bacon():
if request.method == 'GET':
return 'You are probably using GET'
else:
return 'You are probably using POST'
if __name__ == "__main__":
app.run(debug=True, host='localhost')
当我运行这个应用程序时,我的Volley没有连接到我的烧瓶(在运行这个应用程序之前,我已经运行了这个烧瓶)。因此,代码不是检索返回字符串,而是每次运行到Response.ErrorListener。
我正在尝试从烧瓶获得返回字符串到本地主机,并将返回字符串配置为Toast。
发布于 2016-11-02 18:48:27
字符串url = "http://localhost:5000/bacon";//这是FLask的本地主机
实际上,不是。这是运行设备的本地主机。不管是模拟器,还是你的物理Android设备。
您可能还需要在host='0.0.0.0'
上运行烧瓶
您需要使用运行水瓶的计算机的实际IP。安卓仿真器使用10.0.2.2
,我相信
发布于 2018-01-18 14:02:08
我在我的Mac上解决了这个问题,方法是转到> Network并记录我的ip。
然后,我用这个ip地址重新启动了dev appserver。
dev_appserver.py --host="192.168.1.109" app.yaml
我在JsonObjectRequest
声明中也使用了相同的ip地址(作为发出截击请求的一部分)。
现在我可以用Volley打电话到本地的Google。
https://stackoverflow.com/questions/40392978
复制相似问题