本文介绍了Android Volley错误请求代码415与Rest API吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    import flask

    import flask.ext.sqlalchemy

    import flask.ext.restless

    app = flask.Flask(__name__)

    app.config['DEBUG'] = True

    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'

    SQLALCHEMY_TRACK_MODIFICATIONS=True
    db = flask.ext.sqlalchemy.SQLAlchemy(app)

    class Person(db.Model):

        id = db.Column(db.Integer, primary_key=True)
        name = db.Column(db.Unicode, unique=True)
        birth_date = db.Column(db.Date)
    class Computer(db.Model):

        id = db.Column(db.Integer, primary_key=True)
        name = db.Column(db.Unicode, unique=True)
        vendor = db.Column(db.Unicode)
        purchase_time = db.Column(db.DateTime)
        owner_id = db.Column(db.Integer, db.ForeignKey('person.id'))
        owner = db.relationship('Person', backref=db.backref('computers',
                                                             lazy='dynamic'))

    db.create_all()

    manager = flask.ext.restless.APIManager(app, flask_sqlalchemy_db=db)

    manager.create_api(Person, methods=['GET', 'POST', 'DELETE'])

    manager.create_api(Computer, methods=['GET'])

    app.run(host='0.0.0.0', port=5000, debug=True)

客户端:

使用排球发声

 RequestQueue queue = Volley.newRequestQueue(this);

        StringRequest postRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>()
                {
                    @Override
                    public void onResponse(String response) {
                        // response
                        Log.d("Response", response);
                    }
                },
                new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // error

                        Log.d("Error.Response", String.valueOf(error));
                    }
                }
        ) {
            @Override
            protected Map<String, String> getParams()
            {
                Map<String, String>  params = new HashMap<String, String>();
                params.put("name", "Anything");

                return params;
            }
        };
        queue.add( postRequest );

    }

日志输出:

推荐答案

按如下所示提供正确的内容类型

provide the right content type as follows

    // Optional Parameters to pass as POST request
    JSONObject js = new JSONObject();
    try {
        js.put("name","anything");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    // Make request for JSONObject
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(
            Request.Method.POST, url, js,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG, response.toString() + " i am queen");
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                }
        }) {

        /**
         * Passing some request headers
         */
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/json; charset=utf-8");
            return headers;
        }

    };

    // Adding request to request queue
    Volley.newRequestQueue(this).add(jsonObjReq);

}

这篇关于Android Volley错误请求代码415与Rest API吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 11:46