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.