简体   繁体   English

Android中的com.android.volley.AuthFailureError

[英]com.android.volley.AuthFailureError in Android

I am trying to use Google Speech api to translate text from any language to English. 我正在尝试使用Google Speech api将文本从任何语言翻译成英语。 There is no error in api connection but api连接没有错误但是

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服务器文件,它位于服务器中,并且URL_CONNECTION常量提供了此文件的链接

<?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. 当我运行这个活动时,它干杯com.android.volley.AuthFailureError我用谷歌搜索但我没有得到相关的答案。

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 我应该通过Header发送它们,所以我将代码更改为

"@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. 也许它也是你的情况,你需要通过消息的标题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM