【发布时间】:2017-03-11 05:53:11
【问题描述】:
对不起我的英语。我想用这个service。用于确定文本的语言。
请求(卷曲):
curl -X POST -d "outputMode=json" --data-urlencode text@ibm.txt -d "url=http://www.ibm.com/us-en/" "https://gateway-a.watsonplatform.net/calls/text/TextGetLanguage?apikey=%API_KEY%"
我使用 Retrofit 请求。
public interface LanguageDetectionApi {
public static final String ROOT_URL = "https://gateway-a.watsonplatform.net/calls/";
@POST("/text/TextGetLanguage")
Call<List<PostModel>> getData(@Query("apikey") String apikey, @Query("text") String text);
}
创建改造对象:
public class App extends Application {
private static LanguageDetectionApi _languageDetectionApi;
private Retrofit _retrofit;
@Override
public void onCreate() {
super.onCreate();
_retrofit = new Retrofit.Builder()
.baseUrl(_languageDetectionApi.ROOT_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
_languageDetectionApi = _retrofit.create(LanguageDetectionApi.class);
}
public static LanguageDetectionApi getLanguageDetectionApi() {
return _languageDetectionApi;
}
}
并发送请求:
App app = new App();
app.onCreate();
app.getLanguageDetectionApi().getData("4978e60252ae102dfe1341146bb8cc3ec4bbbd78", textForRecognition).enqueue(new Callback<List<PostModel>>() {
@Override
public void onResponse(Call<List<PostModel>> call, Response<List<PostModel>> response) {
List<PostModel> posts = new ArrayList<>();
posts.addAll(response.body());
}
@Override
public void onFailure(Call<List<PostModel>> call, Throwable t) {
Toast.makeText(MainActivity.this, "An error occurred during networking", Toast.LENGTH_SHORT).show();
}
});
我在站点 http://www.jsonschema2pojo.org/ 中生成的 PostModel。
问题: 尽管 apikey 完全有效,但我没有回应。 如何在接口参数“outputMode=json”中指定? 我正确地将 cURL 翻译成 LanguageDetectionApi? 在我看来,LanguageDetectionApi 类中的整个错误。你能帮忙解决这个问题吗?谢谢!
【问题讨论】:
标签: java android url curl retrofit2