Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

com.android.volley.AuthFailureError in Android

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.

like image 415
Sahil Paudel Avatar asked Jan 05 '17 13:01

Sahil Paudel


1 Answers

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.

like image 116
Felipe Viana Avatar answered Sep 23 '22 05:09

Felipe Viana