Retrofit-2 Content-Type Issue

孤者浪人 提交于 2019-12-03 08:33:28

问题


My Api is accepting Content-Type application/json as headers. I set Header perfectly as mentioned in Retrofit Docs.

@Headers("Content-Type: application/json")
@POST("user/classes")
Call<playlist> addToPlaylist(@Body PlaylistParm parm);

But in Request Log it is Returning Content-Type txt/html.So how i should fix this issue? This api works fine in POSTMAN


回答1:


you can try one in below my solutions :

@POST("user/classes")
Call<playlist> addToPlaylist(@Header("Content-Type") String content_type, @Body PlaylistParm parm);

then call

mAPI.addToPlayList("application/json", playListParam);

Or

Create HttpClient object

OkHttpClient httpClient = new OkHttpClient();
        httpClient.networkInterceptors().add(new Interceptor() {
            @Override
            public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
                Request.Builder requestBuilder = chain.request().newBuilder();
                requestBuilder.header("Content-Type", "application/json");
                return chain.proceed(requestBuilder.build());
            }
        });

Then add to retrofit object

Retrofit retrofit = new Retrofit.Builder().baseUrl(BASE_URL).client(httpClient).build();



回答2:


Try this for Retrofit 1.9 and 2.0. For Json Content Type.

@Headers({"Accept: application/json"})
@POST("user/classes")
Call<playlist> addToPlaylist(@Body PlaylistParm parm);

You can add many more i.e

@Headers({
        "Accept: application/json",
        "User-Agent: Your-App-Name",
        "Cache-Control: max-age=640000"
    })



回答3:


Try:

@POST("user/classes")
Call<playlist> addToPlaylist(
@Header("Content-Type") String contentType,
@Body PlaylistParm parm);



回答4:


Retrofit 2.0 drove me nuts. I finally gave up and tried: Android Async Http

And it worked first time. I know what you are thinking that I am looking for stckovflw points or I am somehow flaming Retrofit. I am not. My use case was to call my bitbucket account with a simple inline authentication: https://[username]:[password]@api.bitbucket.org/2.0/repositories/[username]

With Retrofit I was getting another response. An Http 200 but not the payload I would expect, as you would get from Postman. I tried everything I could find to get it to work. Wasted I think 2 days on it or more. I guess you might be thinking, well, you did not form your baseUrl correctly...or the URI for the resource. Look at the above, how less complicated can it be...I tried shortening the URI to the min. and setting the baser URL to the longest part of the above; reminder: I got an Http 200 back. But not the correct response payload!

A colleague of mine suggested using the asynch lib. and given all the recommendations for Retrofit and the time I had spent, I was skeptical. It worked first time!!




回答5:


Here's a solution that works as of retrofit2(v2.5) and okhttp3. Works especially well if you will add these headers to a number of requests

OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
        httpClientBuilder.addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request.Builder requestBuilder = chain.request().newBuilder();
                requestBuilder.header("Content-Type", "application/json");
                requestBuilder.header("Accept", "application/json");
                return chain.proceed(requestBuilder.build());
            }
        });

 OkHttpClient httpClient = httpClientBuilder.build();
 Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(httpClient)
                .build();



回答6:


maybe you need '/' and is best not to change the Content-Type by youself. just like this:

@POST("user/classes/")
Call<playlist> addToPlaylist(@Body PlaylistParm parm);


来源:https://stackoverflow.com/questions/35747524/retrofit-2-content-type-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!