如何使用params向Android烧瓶服务器发送帖子请求?

时间:2018-06-14 09:19:42

标签: java android python flask request

我正在尝试使用AsyncHttpClient从Android应用程序发送到python flask服务器的帖子请求,但参数未被识别,为什么? 当服务器收到请求时,它看起来像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() {

            });


}

烧瓶服务器是:

@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)

0 个答案:

没有答案