【问题标题】:Android (Java) - POST (with headers) with Retrofit 2.1.0Android (Java) - 带有 Retrofit 2.1.0 的 POST(带标题)
【发布时间】:2017-03-23 20:47:16
【问题描述】:

我需要将一个名为 SMMRY 的 RESTful API 集成到我的 Android 应用程序中。此 API 对发送给它的 URL 或文本块进行自动文本摘要。 我正在使用 Retrofit 2.1.0 作为网络库。

我已经设法让 URL 文本摘要完美运行,所以当我传递 URL 时,它会执行文本摘要并返回给我。

但是我无法通过发送适当的参数(包括标题等)来正确实现 POST 请求,因此它可以对文本块进行摘要。

我认为问题在于我没有正确传递标题。我尝试了 3 种不同的方法来传递它们,但每次我都得到“空”值响应。首先,我尝试在 POST 请求之前添加 @Headers (我认为这是正确的方法);其次,我尝试将标头作为参数传递;最后,通过在 OkHttp 拦截器中管理请求标头。但是没有任何效果,或者我的代码中的某些内容没有很好地实现......

1 页 API 文档(带有 PHP 中的示例实现):http://smmry.com/api

以下是完整代码(我已经注释了我尝试传递标头的所有方式,因此很容易想出什么是正确的方式或我应该如何纠正它):

MainActivity.java

public class MainActivity extends AppCompatActivity {

    public static final String SM_API_KEY= "XXXXXXXXX"; // My API Key (which can be obtained by their site )

    public static final String URL="http://www.abola.pt/nnh/ver.aspx?id=639170";

    public static final String longText= "Robert Catesby (1572?–1605) was the leader of the failed Gunpowder Plot of 1605, " +
            "commemorated in Great Britain every 5 November as Guy Fawkes Night. His family were prominent recusant Catholics." +
            " A letter sent anonymously to William Parker, 4th Baron Monteagle, alerted the authorities, and on the eve of the planned explosion, " +
            "during a search of Parliament, Fawkes was found guarding the barrels of gunpowder, and arrested. Catesby and the remaining plotters made a " +
            "stand against a 200-strong company of armed men at Holbeche House in Staffordshire, where he was shot and killed. As a warning to others, his body" +
            " was exhumed and his head exhibited outside Parliament";


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

        if (SM_API_KEY.isEmpty()){
            Log.d("SM_KEY","SM_API_KEY not available");
            return;
        }

        SmmryAPIInterface apiService =
                SmmryAPIClient.getClient().create(SmmryAPIInterface.class);// No headers

        SmmryAPIInterface apiService2 =
                SmmryAPIClient.getClient2().create(SmmryAPIInterface.class); // client with headers, NOT USED RIGHT NOW

        /* Encode the URL when summarizing by URL - which is working OK*/
        String link = null;
        try {
            link = URLEncoder.encode(URL, "utf-8");
        } catch (UnsupportedEncodingException e) {

        }


        // Summary by URL: Working properly, the following commented lines are to call to URL summary ...

        /*Call<SmmaryResponse> call = apiService.getSummaryURL(SM_API_KEY,1,null,null,null,link);
        call.enqueue(new Callback<SmmaryResponse>() {
            @Override
            public void onResponse(Call<SmmaryResponse>call, Response<SmmaryResponse> response) {
                String sm_api_content = String.valueOf(response.body().getSm_api_content());
                //String sm_api_content = response.errorBody().toString();

                Log.e("Resumo", sm_api_content);
            }

            @Override
            public void onFailure(Call<SmmaryResponse>call, Throwable t) {
                // Log error here since request failed
                Log.e("Erro", t.toString());
            }
        });*/


        // Summary by Text : THE PROBLEM IS HERE!:
        String header = "100-continnue";
        Call<SmmaryResponse> call2 = apiService.getSummaryText(SM_API_KEY,
                                                                /*header,*/  // TRIED TO PASS THE HEADER HERE, BUT DID NOT WORK ...
                                                                longText, 2, null, null, null);
        call2.enqueue(new Callback<SmmaryResponse>() {
            @Override
            public void onResponse(Call<SmmaryResponse>call, Response<SmmaryResponse> response) {
                String sm_api_content = String.valueOf(response.body().getSm_api_content());

                Log.e("Summary longText", sm_api_content);
            }

            @Override
            public void onFailure(Call<SmmaryResponse>call, Throwable t) {
                // Log error here since request failed
                Log.e("Erro", t.toString());
            }
        });

    }
}

SmmaryResponse.java

public class SmmaryResponse {
    /*
        Currently in this class I am only using getSm_api_content(), which contains the actual summary ... This is the model class
     */

    private String sm_api_content;

    public SmmaryResponse(String sm_api_content) {
        this.sm_api_content = sm_api_content;
    }

    // the only method I am currently using, to get the summary
    public String getSm_api_content() {
        return sm_api_content;
    }
}

SmmryAPIClient.java

public class SmmryAPIClient {

    /*
      To send network requests to an API, we need to use the Retrofit Builder class and specify the base URL for the service.
     */

    public static final String BASE_URL = "http://api.smmry.com/";

    private static Retrofit retrofit = null;
    private static Retrofit retrofit2 = null;


    /*Method to get the client*/
    public static Retrofit getClient() {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .client(getRequestHeader())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }

    /* Method to set timeout in Retrofit library */
    private static OkHttpClient getRequestHeader() {
        OkHttpClient httpClient = new OkHttpClient.Builder()
                .connectTimeout(100, TimeUnit.SECONDS)
                .writeTimeout(100, TimeUnit.SECONDS)
                .readTimeout(300, TimeUnit.SECONDS)
                .build();
        return httpClient;
    }

    /*The following two methods have the purpose to create client, but this time with Headers */
    public static Retrofit getClient2() {
        if (retrofit2 == null) {
            retrofit2 = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .client(getRequestHeader2())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit2;
    }

    /* Method to set timeout + headers in Retrofit library */
    private static OkHttpClient getRequestHeader2() {
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        httpClient.addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Interceptor.Chain chain) throws IOException {
                Request original = chain.request();

                Request request = original.newBuilder()
                        //.addHeader("Content-Type", "text/plain; charset=utf-8")
                        .addHeader("Expect:", "100-continue") //  // TRIED TO PASS THE HEADER HERE, BUT DID NOT WORK ...
                        //.method(original.method(), original.body())
                        .build();
                return chain.proceed(request);
            }
        })
                .connectTimeout(100, TimeUnit.SECONDS)
                .writeTimeout(100, TimeUnit.SECONDS)
                .readTimeout(300, TimeUnit.SECONDS);
        OkHttpClient client = httpClient.build();
        return client;
    }
}

SmmryAPIInterface.java

public interface SmmryAPIInterface {

    /*
    * The following two methods are implemented:
    *   getSummaryURL: method to request for the summary by providing an URL (SM_URL parameter to send URL)
    *   getSummaryText: method to request for the summary by providing a block of text (sm_api_input parameter to send block of text)
    *   Use of any optional parameters provided by Smmry, like SM_LENGTH ,SM_WITH_BREAK, etc is implemented
    */

    /* Pass null value strictly to optional parameters if not needed.*/
    @POST("/")
    Call<SmmaryResponse> getSummaryURL(@Query("SM_API_KEY") String apiKey,
                                       @Query("SM_LENGTH") Integer length,
                                       @Query("SM_WITH_BREAK") Boolean wbreak,
                                       @Query("SM_QUOTE_AVOID") Boolean quote,
                                       @Query("SM_KEYWORD_COUNT") Integer N,
                                       @Query("SM_URL") String urlSite);


    /* Pass null value strictly to optional parameters if not needed.*/

    //@Headers("Expect: 100-continue") // TRIED TO WRITE THE HEADER HERE, BUT DID NOT WORK ...
    @POST("/")
    Call<SmmaryResponse> getSummaryText(@Query("SM_API_KEY") String apiKey,
                                        //@Header("Expect:") String header, // TRIED TO WRITE THE HEADER HERE, BUT DID NOT WORK ...
                                        @Query("sm_api_input") String bText,
                                        @Query("SM_LENGTH") Integer length,
                                        @Query("SM_WITH_BREAK") Boolean wBreak,
                                        @Query("SM_QUOTE_AVOID") Boolean quote,
                                        @Query("SM_KEYWORD_COUNT") Integer N);
}

我现在坚持了几天,尝试了很多东西,但不明白我做错了什么。任何帮助将不胜感激。 谢谢。

【问题讨论】:

    标签: android post http-headers retrofit2


    【解决方案1】:

    我认为第一个和第三个选项都是正确的。

    请注意,在第一种情况下,当您仅添加注释 @Headers 时,它只会为一次调用添加,但如果您创建拦截器标头,则将在所有调用中添加使用此 OkHttpClient。

    但我认为你的问题出在不同的地方。我认为您误解了 SMMRY 文档。 “sm_api_input”不是查询参数。

    请尝试以下代码:

    @FormUrlEncoded
    @POST("/")
    Call<SmmaryResponse> getSummaryText(@Query("SM_API_KEY") String apiKey,
                                        @Field("sm_api_input") String bText,
                                        @Query("SM_LENGTH") Integer length,
                                        @Query("SM_WITH_BREAK") Boolean wBreak,
                                        @Query("SM_QUOTE_AVOID") Boolean quote,
                                        @Query("SM_KEYWORD_COUNT") Integer N);
    

    最好的问候, 马辛

    【讨论】:

    • 感谢马尔辛的帮助!您的回答解决了问题:)
    猜你喜欢
    • 2019-11-29
    • 1970-01-01
    • 2014-08-07
    • 1970-01-01
    • 2012-06-01
    • 2021-08-11
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    相关资源
    最近更新 更多