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 );
}
04-29 11:42:24.556 1890-1946 /? I / Icing:完成索引F3642025687382E430F3465743F12480D56AAC66 04-29 11:43:32.123 3147-3196 /? E / Volley:[157] BasicNetwork。 performRequest:http://IP:5000/api/person的意外响应代码415 04-29 11:43:32.132 3147-3147 /? D / Error.Response:com.android.volley.ServerError 04-29 11:45:19.365 1298-1311 /? I / UsageStatsService:User [0]将使用统计信息刷新到磁盘
答案 0 :(得分:4)
提供正确的内容类型如下
// 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);
}
答案 1 :(得分:2)
415是错误媒体类型的错误代码。当服务器需要json时,您将以纯文本形式发送正文或帖子请求。找出您应该发送的内容类型,并将帖子正文添加到该类型。
@Override
public byte[] getBody() {
return 'your json string'.getBytes();
}
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
答案 2 :(得分:0)
如果您使用的是Kotlin,这是我实现的代码段
val jsonBody = JSONObject()
jsonBody.put("email", email)
jsonBody.put("password", password)
val requestBody = jsonBody.toString()
val registerRequest =
object : StringRequest(Method.POST, URL_REGISTER, Response.Listener { response ->
print(response)
complete(true)
}, Response.ErrorListener { error ->
Log.d("Error", "not possible to register at this time . $error")
complete(false)
}) {
override fun getBodyContentType(): String {
return "application/json; charset=utf-8";
}
override fun getBody(): ByteArray {
return requestBody.toByteArray()
}
}
Volley.newRequestQueue(context).add(registerRequest)
}