每次使用Android中的Retrofit调用onFailure的Google Translation API的REST请求

时间:2018-04-15 08:53:51

标签: 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();
    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;
}
}

类APIResponse,数据和翻译有三种不同的.java文件。

这是我的REST界面:

public interface TranslationRESTInterface {

@POST("?")
public Call<APIResponse> translateWord(@Query("q") String inputWord,
                                       @Query("target") String targetLang,
                                       @Query("key") String apiKey);
}

这是我在我的主要活动中执行请求的代码(暂时,只是为了验证一切正常):

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");

                }
            });

}
}

正如我在Logcat和我的活动中所看到的,Call不会给我一个响应,经过一段时间它总是被称为onFailure方法。 我试图在REST接口和其他更改中更改POST请求,但我无法理解问题所在或在哪里查看。 我真的希望你们中的一些人能给我一个提示! 非常感谢!

0 个答案:

没有答案