Java 每次在Android调用失败时使用改型的谷歌翻译API的REST请求

Java 每次在Android调用失败时使用改型的谷歌翻译API的REST请求,java,android,rest,retrofit,google-translation-api,Java,Android,Rest,Retrofit,Google Translation Api,我在这里发布我的代码: public class APIResponse { @SerializedName("data") @Expose private Data data; /** * No args constructor for use in serialization * */ public APIResponse() { } /** * * @param data */ public APIResponse(Data data) { super();

我在这里发布我的代码:

public class APIResponse {

@SerializedName("data")
@Expose
private Data data;

/**
 * No args constructor for use in serialization
 *
 */
public APIResponse() {
}

/**
 *
 * @param data
 */
public APIResponse(Data data) {
    super();
    this.data = data;
}

public Data getData() {
    return data;
}

public void setData(Data data) {
    this.data = data;
}
}


public class Data {

@SerializedName("translations")
@Expose
private List<Translation> translations = null;

/**
 * No args constructor for use in serialization
 *
 */
public Data() {
}

/**
 *
 * @param translations
 */
public Data(List<Translation> translations) {
    super();
    this.translations = translations;
}

public List<Translation> getTranslations() {
    return translations;
}

public void setTranslations(List<Translation> translations) {
    this.translations = translations;
}
}


public class Translation {

@SerializedName("translatedText")
@Expose
private String translatedText;
@SerializedName("detectedSourceLanguage")
@Expose
private String detectedSourceLanguage;

/**
 * No args constructor for use in serialization
 *
 */
public Translation() {
}

/**
 *
 * @param detectedSourceLanguage
 * @param translatedText
 */
public Translation(String translatedText, String detectedSourceLanguage) {
    super();
    this.translatedText = translatedText;
    this.detectedSourceLanguage = detectedSourceLanguage;
}

public String getTranslatedText() {
    return translatedText;
}

public void setTranslatedText(String translatedText) {
    this.translatedText = translatedText;
}

public String getDetectedSourceLanguage() {
    return detectedSourceLanguage;
}

public void setDetectedSourceLanguage(String detectedSourceLanguage) {
    this.detectedSourceLanguage = detectedSourceLanguage;
}
}
公共类响应{
@SerializedName(“数据”)
@暴露
私人数据;
/**
*没有用于序列化的args构造函数
*
*/
公众回应({
}
/**
*
*@param数据
*/
公众回应(数据){
超级();
这个数据=数据;
}
公共数据getData(){
返回数据;
}
公共无效设置数据(数据){
这个数据=数据;
}
}
公共类数据{
@序列化名称(“翻译”)
@暴露
私有列表翻译=null;
/**
*没有用于序列化的args构造函数
*
*/
公共数据(){
}
/**
*
*@param翻译
*/
公共数据(列表翻译){
超级();
这个。翻译=翻译;
}
公共列表getTranslations(){
返回翻译;
}
公共翻译(列表翻译){
这个。翻译=翻译;
}
}
公共课翻译{
@SerializedName(“translatedText”)
@暴露
私有字符串translatedText;
@SerializedName(“detectedSourceLanguage”)
@暴露
私有字符串检测源语言;
/**
*没有用于序列化的args构造函数
*
*/
公开翻译(){
}
/**
*
*@param detectedSourceLanguage
*@param translatedText
*/
公共翻译(String translatedText,String detectedSourceLanguage){
超级();
this.translatedText=translatedText;
this.detectedSourceLanguage=detectedSourceLanguage;
}
公共字符串getTranslatedText(){
返回翻译文本;
}
public void setTranslatedText(字符串translatedText){
this.translatedText=translatedText;
}
公共字符串getDetectedSourceLanguage(){
返回检测到的源语言;
}
public void setDetectedSourceLanguage(字符串detectedSourceLanguage){
this.detectedSourceLanguage=detectedSourceLanguage;
}
}
APIResponse、Data和Translation类位于三个不同的.java文件中

这是我的REST界面:

public interface TranslationRESTInterface {

@POST("?")
public Call<APIResponse> translateWord(@Query("q") String inputWord,
                                       @Query("target") String targetLang,
                                       @Query("key") String apiKey);
}
公共接口TranslationRESTInterface{
@职位(“?”)
公共调用translateWord(@Query(“q”)字符串输入字,
@查询(“目标”)字符串targetLang,
@查询(“键”)字符串(键);
}
这是我在主要活动中执行请求的代码(目前,只是为了验证一切正常):

public类MainActivity扩展了AppCompatActivity{
私有TranslationRESTInterface-restInterface;
最终上下文=此;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
最终文本视图文本视图翻译=findViewById(R.id.textViewAAA);
改装改装=新建改装.Builder().baseUrl(Commons.hostName)
.addConverterFactory(GsonConverterFactory.create()).build();
restInterface=reformation.create(TranslationRESTInterface.class);
restInterface.translateWord(“house”、“it”、Commons.apiKey)
.enqueue(新的回调函数(){
@凌驾
公共void onResponse(调用、响应){
Log.d(“退出”、“正常”);
APIResponse=response.body();
Data dataResponse=apiResponse.getData();
列表=
dataResponse.getTranslations();
字符串a=list.get(0.getTranslatedText().toString();
textViewTranslation.setText(a);
Log.d(“trad”,a);
Toast Toast=Toast.makeText(上下文,a,0);
toast.setGravity(重力。水平重心|垂直重心|
,0,0);
toast.show();
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.d(“退出”、“失败”);
}
});
}
}
正如我在Logcat和我的活动中所看到的,调用没有给我响应,经过一段时间后,它总是被称为方法onFailure。 我曾尝试在RESTinterface中更改POST请求和其他更改,但我无法理解问题在哪里,也无法找到问题所在。 我真的希望你们中的一些人能给我一个提示!!!
非常感谢

确保您可以访问网络。例如,确保您拥有访问internet的适当权限I添加了internet权限,并在我的手机上尝试了该应用程序,认为可能是模拟器的连接问题当onFailure()被触发时,请检查logcat以了解其失败的任何线索(例如异常消息)。
public class MainActivity extends AppCompatActivity {

private TranslationRESTInterface restInterface;

final Context context = this;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getSupportActionBar().hide();
    setContentView(R.layout.activity_main);


    final TextView textViewTranslation = findViewById(R.id.textViewAAA);

    Retrofit retrofit = new Retrofit.Builder().baseUrl(Commons.hostName)
            .addConverterFactory(GsonConverterFactory.create()).build();

    restInterface = retrofit.create(TranslationRESTInterface.class);

    restInterface.translateWord("house", "it", Commons.apiKey)
            .enqueue(new Callback<APIResponse>() {

                @Override
                public void onResponse(Call<APIResponse> call, Response<APIResponse> response) {


                    Log.d("out","ok");

                    APIResponse apiResponse = response.body();

                    Data dataResponse = apiResponse.getData();

                    List<Translation> list =
                            dataResponse.getTranslations();

                    String a = list.get(0).getTranslatedText().toString();

                    textViewTranslation.setText(a);

                    Log.d("trad",a);

                    Toast toast = Toast.makeText(context, a ,0);
                    toast.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL
                            ,0,0);
                    toast.show();
                }

                @Override
                public void onFailure(Call<APIResponse> call, Throwable t) {

                    Log.d("out","fail");

                }
            });

}
}