0

我在这里发布我的代码:

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、Data 和 Translation 类位于三个不同的 .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 请求和其他更改,但我无法理解问题出在哪里或在哪里查看。我真的希望你们中的一些人能给我一个提示!!!!非常感谢!

4

0 回答 0