Java 如何从Android向flask服务器发送带有参数的post请求?
我正试图使用Java 如何从Android向flask服务器发送带有参数的post请求?,java,android,python,flask,request,Java,Android,Python,Flask,Request,我正试图使用AsyncHttpClient从android应用程序向python flask服务器发出post请求,但无法识别参数,为什么? 当服务器收到请求时,它看起来像是POST/testing HTTP/1.1 这是Android代码: public void encodeImagetoString() { new AsyncTask<Void, Void, String>() { protected void onPreExecute() {
AsyncHttpClient
从android应用程序向python flask服务器发出post请求,但无法识别参数,为什么?
当服务器收到请求时,它看起来像是POST/testing HTTP/1.1
这是Android代码:
public void encodeImagetoString() {
new AsyncTask<Void, Void, String>() {
protected void onPreExecute() {
};
@Override
protected String doInBackground(Void... params) {
BitmapFactory.Options options = null;
options = new BitmapFactory.Options();
options.inSampleSize = 3;
bitmap = BitmapFactory.decodeFile(imgPath,
options);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byte_arr = stream.toByteArray();
encodedString = Base64.encodeToString(byte_arr, 0);
return "";
}
@Override
protected void onPostExecute(String msg) {
AsyncHttpClient client = new AsyncHttpClient();
HashMap<String, String> param = new HashMap<String, String>();
param.put("image", encodedString);
params = new RequestParams(param);
triggerImageUpload();
}
}.execute(null, null, null);
}
public void triggerImageUpload() {
makeHTTPCall();
}
public void makeHTTPCall() {
AsyncHttpClient client = new AsyncHttpClient();
Log.w("Parametrul este:",params.toString());
client.post("http://localhost/testing",
params, new AsyncHttpResponseHandler() {
});
}
您有一个名为
params
的变量,其中包含RequestParams
,然后您调用triggermageupload()
,其中方法不确切知道params
是什么。您可以更改triggerImageUpload(RequestParams params)
,类似于makeHTTPCall(..)
,以使其成为类的“工作参数”字段。它也可以从makeHTTPCall中看到。flask代码需要一个image
变量,但它没有在路由中指定,因此我相信此代码会中断。另外,请记住,您可以使用request.args.get(“image”)
获取?
后面的任何参数。我修复了该错误,但问题仍然存在。请求未获取参数。
@app.route('/testing', methods=['POST','GET'])
def testing(image):
if request.method == 'POST':
return image
#return "Image was received"
if request.method == 'GET':
return "Get request"
if __name__ == '__main__':
app.run(threaded=True)