-1

I'm trying to communicate between Android and a Flask server I've set up on AWS. I have some data I've put inside a JSONObject, and I'm sending that over a POST request to the server.

The Flask server receives this JSON, parses it, and even does the appropriate manipulations. However, when I send a Response back to Android, it doesn't seem to identify it at all.

I believe I'm going wrong in the Flask return part, but I don't know where exactly.

Here's the Flask return code:

parsed_results = parse_text(results)

#json_x = json.dumps({'id':str(status.inserted_id), 'Result':'OK', 'data': parsed_results})
return Response(json.dumps({'id': str(status.inserted_id), 'Result': 'OK', 'data': parsed_results, 'raw_data': results}), mimetype='application/json'), 200


if __name__ == "__main__":
    app.run(host='0.0.0.0', debug=True)

Here's the Android Listener code:

JsonObjectRequest request = new JsonObjectRequest(
            Request.Method.POST,
            URL,
            data,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d("JSON_TAG", response.toString());
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    error.printStackTrace();
                }
            }
    )

    {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            return super.getParams();
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            //return super.getHeaders();
            Map<String,String> headers = new HashMap<>();
            headers.put("Content-Type","application/json");
            return headers;
        }
};

The Android application just stays idle. Where am I going wrong? Any help would be greatly appreciated. Thanks!

0

1 Answer 1

-1

I solved the issue, guys. The problem was very apparent - a mismatched return statement from the server. By using jsonify, I was able to return the data to the server.

from flask import jsonify

results = parse_results(data)

return jsonify(results)

Worked like a charm!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.