我想将这些值传递给我的烧瓶并接收响应。 以下是我如何构建我的表单体,以及恢复其响应的请求和方法:
OkHttpClient okHttpClient;
RequestBody formBody;
Request request;
okHttpClient= new OkHttpClient();
formBody=new FormBody.Builder().add("Name",Date_Time).add("Humidity_value",hum).build();
request=new Request.Builder().url("http://10.0.2.2:5000/api").post(formBody).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
Log.d("message","Network not found");
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
Log.d("message",response.body().string());
}
});
我放置了依赖项并导入了所需的库,但在Android Studio中我得到了错误提示:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.</p>
在我的烧瓶方面还有一个错误:
Name = request.form('Name')
TypeError: 'ImmutableMultiDict' object is not callable
127.0.0.1 - - [27/Mar/2021 15:12:01] "POST /api HTTP/1.1" 500 -
这是我的代码:
@app.route('/api', methods=['POST'])
def predict():
NameD = request.form('Name') **#the console said the error is here**
NameD = pd.to_datetime(NameD).value
NameD = float(NameD)
Humidity_value = request.form('Humidity_value')
inputs = np.array([[NameD,Humidity_value]])
prediction = model.predict(inputs)
output = prediction[0]
return (str(output))
if __name__ == "__main__":
app.run(host="0.0.0.0")
如何解决TypeError: 'ImmutableMultiDict' object is not callable
错误
request.form
是一个类似于python字典的ImmutableMultiDict
,不能使用round brackets ()
从表单检索数据。而是使用square brackets []
。将request.form('key')
的所有实例更改为request.form['key']
。或者您可以使用request.form.get('key')
这里使用圆括号()与尝试调用函数类似。由于我们不能调用
ImmutableMultiDict
,因此会发生错误。android studio内部的错误很可能是由于flask脚本中的错误而发生的你的预测函数应该是
相关问题 更多 >
编程相关推荐