Android(Java) - 带有Retrofit 2.1.0的POST(带标题)

时间:2016-11-09 22:39:10

标签: android post http-headers retrofit2

我需要将名为SMMRY的 RESTful API 集成到我的Android App中。此API会对发送给它的URL或文本块执行自动文本摘要。 我使用 Retrofit 2.1.0 作为网络库。

我已经成功地将URL文本摘要工作,所以当我传递URL时,它会执行文本摘要并提供给我。

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

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

API的1页文档(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);
}

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

1 个答案:

答案 0 :(得分:1)

在我看来,第一和第三选项都是正确的。

请注意,在第一种情况下,当您只添加注释 @Headers 时,它仅添加一个调用,但如果您创建Interceptor标头将添加到所有调用中使用此 OkHttpClient < / em>的

但我认为你的问题出在不同的地方。我想你误解了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);

祝你好运, 马尔钦