Android volley error request code 415 with Rest API?

前端 未结 3 1167
春和景丽
春和景丽 2020-12-22 01:20

Server side:

    import flask

    import flask.ext.sqlalchemy

    import flask.ext.restless

    app = flask.Flask(__name__)

    app.config[\'DEBUG\'] =         


        
相关标签:
3条回答
  • 2020-12-22 01:58

    415 is the error code for wrong media type. You are sending the body or your post request in plain text when the server expects json. Find out what type of content you should send and make the post body to that type.

    @Override
    public byte[] getBody() {
        return 'your json string'.getBytes(); 
    }
    
    @Override
    public String getBodyContentType() {
        return "application/json; charset=utf-8";
    }
    
    0 讨论(0)
  • 2020-12-22 02:02

    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);
    
    }
    
    0 讨论(0)
  • 2020-12-22 02:10

    if you are using Kotlin, here is the snippet I implemented

     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)
    }
    
    0 讨论(0)
提交回复
热议问题