com.android.volley.AuthFailureError in Android
Asked Answered
G

1

7

I am trying to use Google Speech api to translate text from any language to English. There is no error in api connection but

volley

is giving me error. Thanks for help. My Activity code:

 private void Translate(String s) {

    //trim out translate
    final String stringToTranslate = s.substring(9);

    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_CONNECTION,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Toast.makeText(MainActivity.this, response, Toast.LENGTH_LONG).show();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
                }
            }){
        @Override
        protected Map<String, String> getParams() {
              Map<String, String> params = new HashMap<>();
                                  params.put(TO_TRANSLATE, stringToTranslate);
              return params;
        }
    };
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

My PHP Server file, it is in the server and link to this file is provided by URL_CONNECTION Constant

<?php

use Google\Cloud\Translate\TranslateClient;

if($_SERVER['REQUEST_METHOD']=='POST'){

$fb = $_POST['toTranslate'];

require 'vendor/autoload.php';

$translate = new TranslateClient([
    'key' => 'My TranslateApi key'
]);

        $result = $translate->translate($fb, [
                  'target' => 'en'
        ]);

$conversion =  $result['text'];

echo $conversion;
}

?>

When i run this activity it toast com.android.volley.AuthFailureError I googled but i didn't get the relevant answers.

Gettogether answered 5/1, 2017 at 13:31 Comment(1)
Anyone please i am stuck in this.Gettogether
S
1

I've had a similar problem. It turned out I was overriding the wrong method to pass parameters. I should send them by the Header, so I changed my code to

"@override public Map<String, String> getHeaders()" 

instead of

"@override protected Map<String, String> getParams()"

Maybe it's also your case, you need pass by the header of the message.

Salley answered 16/3, 2018 at 22:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.