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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With