【问题标题】:How to convert cURL to retrofit correct form?如何将 cURL 转换为改造正确的形式?
【发布时间】: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


    【解决方案1】:

    更改网址代码如下:

    public interface LanguageDetectionApi {
        public static final String ROOT_URL = "https://gateway-a.watsonplatform.net";
    
        @POST("/calls/text/TextGetLanguage")
        Call<List<PostModel>> getData(@Query("apikey") String apikey, @Query("text") String text);
    }
    

    基本 url 应该是唯一的主机名。

    【讨论】:

      猜你喜欢
      • 2021-12-29
      • 1970-01-01
      • 2018-06-12
      • 2023-03-14
      • 2021-05-09
      • 2021-04-15
      • 1970-01-01
      • 2019-09-01
      • 1970-01-01
      相关资源
      最近更新 更多