问题描述
我使用Flask将NLP模型部署为API。现在,我想从我简单的Android应用程序中调用API来处理一些文本并返回预测,但是当Android应用程序使用Volley执行HTTP请求时,由于某种原因它不会在URL中添加参数。这是代码:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// button
val showButton = findViewById<Button>(R.id.showInput)
// text
val editText = findViewById<EditText>(R.id.editText)
// Setting On Click Listener
showButton.setOnClickListener {
// user input
val text = editText.text
// Instantiate the RequestQueue.
val queue = Volley.newRequestQueue(this)
val url = "http://192.168.100.12:5000/"
// Request a string response from the provided URL.
val request = object : StringRequest(Request.Method.POST,url,Response.Listener {
response ->
editText.setText(response.toString())
},Response.ErrorListener { error ->
Log.i("error ",error.toString())
editText.setText("Volley error: $it ")
}) {
override fun getBodyContentType(): String {
return "application/json"
}
@Throws(AuthFailureError::class)
override fun getBody(): ByteArray {
val params = HashMap<String,String>()
params.put("text",text.toString())
return params.toString().toByteArray()
}
}
// Add the request to the RequestQueue.
queue.add(request)
// Showing the response
Toast.makeText(this,text,Toast.LENGTH_SHORT).show()
}
}
}
从本质上讲,我希望URL为“ http://192.168.100.12:5000/?text= AttributeError: 'NoneType' object has no attribute 'lower'
错误响应代码为500。
我在浏览器和Postman上都测试了Flask API,它工作正常,但以防万一我将代码留在这里:
from flask import Flask,render_template,url_for,request,jsonify
from sklearn.externals import joblib
import traceback
app = Flask(__name__)
@app.route("/",methods=['GET','POST'])
def predict():
try:
tweet = request.args.get('text')
model = open('model.pkl','rb')
model = joblib.load(model)
prediction = model.predict([tweet])
if prediction == [0]:
return 'This tweet does not violate our Community Guidelines'
else:
return 'This tweet violates out Community Guidelines for hate speech'
except:
return traceback.format_exc()
if __name__ == '__main__':
app.run(host = '192.168.100.12',debug=True)
到目前为止,我还尝试了this帖子中的代码,以及将GET方法与getParams()
函数一起使用,而不是getBody()
,如下所示,但是没有一个起作用。 / p>
@Throws(AuthFailureError::class)
override fun getParams(): Map<String,String>? {
val params: HashMap<String,String> = HashMap()
params["text"] = text.toString()
return params
}
能帮我解决这个问题吗?我将非常感谢。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)