как можно отправить JSON? - PullRequest
0 голосов
/ 14 мая 2019

Я пытаюсь отправить JSON в веб-сервис, используя Google Cloud API, но получаю сообщение об ошибке ниже;

ОШИБКА == 415 "E / Volley: [293] BasicNetwork.performRequest: неожиданный код ответа 415 для http://172.17.1.169:8080/api/save"

Вот код:

mTextMod.setText(text);
JSONObject jsonParam = new JSONObject();
try {
    jsonParam.put("text", text);
    jsonParam.put("language", "gl-ES");
    jsonParam.put("voice", "Vocalizer Expressive Carmela Harpo 22kHz");
    jsonParam.put("rate", null);
    jsonParam.put("volume", "90");
    System.out.println("patata ->" + jsonParam.toString());
} catch (JSONException e) {
    e.printStackTrace();
}

RequestQueue queue = Volley.newRequestQueue(MainActivity.this);

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
    Request.Method.GET,
    url,
    jsonParam,
    new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            Toast.makeText(MainActivity.this, response.toString(),
            Toast.LENGTH_LONG).show();
            System.out.println(response.toString());
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
            System.out.println(error.toString());
            NetworkResponse networkResponse = error.networkResponse;
        }
        if (networkResponse != null) {
            Log.e("Volley", "Error. HTTP Status Code:" + networkResponse.statusCode);
        }
        if (error instanceof TimeoutError) {
            Log.e("Volley", "TimeoutError");
        } else if (error instanceof NoConnectionError) {
            Log.e("Volley", "NoConnectionError");
        } else if (error instanceof AuthFailureError) {
            Log.e("Volley", "AuthFailureError");
        } else if (error instanceof ServerError) {
            Log.e("Volley", "ServerError");
        } else if (error instanceof NetworkError) {
            Log.e("Volley", "NetworkError");
        } else if (error instanceof ParseError) {
            Log.e("Volley", "ParseError");
        }
    }
});

queue.add(jsonObjectRequest);

1 Ответ

1 голос
/ 14 мая 2019
     /**
     * Pass 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;
    }

/ ** * как показано ниже * /

JSONObject jsonParam = new JSONObject ();

                            try {
                                jsonParam.put("text", text);
                                jsonParam.put("language", "gl-ES");
                                jsonParam.put("voice", "Vocalizer Expressive Carmela Harpo 22kHz");
                                jsonParam.put("rate", null);
                                jsonParam.put("volume", "90");
                                System.out.println("patata ->" + jsonParam.toString());
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }


                            RequestQueue queue = Volley.newRequestQueue(MainActivity.this);

                            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
                                    (Request.Method.GET, url, jsonParam, new Response.Listener<JSONObject>(){

                                        @Override
                                        public void onResponse(JSONObject response) {
                                            Toast.makeText(MainActivity.this, response.toString(), Toast.LENGTH_LONG).show();
                                            System.out.println(response.toString());
                                        }
                                    }, new Response.ErrorListener(){

                                        @Override
                                        public void onErrorResponse(VolleyError error) {
                                            Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
                                            System.out.println(error.toString());
                                            NetworkResponse networkResponse = error.networkResponse;
                                            if (networkResponse != null) {
                                                Log.e("Volley", "Error. HTTP Status Code:" + networkResponse.statusCode);
                                            }

                                            if (error instanceof TimeoutError) {
                                                Log.e("Volley", "TimeoutError");
                                            } else if (error instanceof NoConnectionError) {
                                                Log.e("Volley", "NoConnectionError");
                                            } else if (error instanceof AuthFailureError) {
                                                Log.e("Volley", "AuthFailureError");
                                            } else if (error instanceof ServerError) {
                                                Log.e("Volley", "ServerError");
                                            } else if (error instanceof NetworkError) {
                                                Log.e("Volley", "NetworkError");
                                            } else if (error instanceof ParseError) {
                                                Log.e("Volley", "ParseError");
                                            }

                                        }

                                    })

{

    /**
     * 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;
    }

};

                            queue.add(jsonObjectRequest);
...