POST请求中的Android Retrofit2错误422

时间:2017-03-21 11:14:23

标签: java android retrofit2 okhttp3 http-status-code-422

我想通过改造向服务器发出POST请求,但我总是得到错误422。 我只是想知道我是做错了什么还是服务器故障。

改装电话:

OkHttpClient.Builder duplicateTimeSessionClient = new kHttpClient.Builder();

duplicateTimeSessionClient.addInterceptor(newInterceptor() {
    @Override
    public okhttp3.Response intercept(Chain chain) throws IOException {
        Request original = chain.request();

        Request request = original.newBuilder()
                                    .header("Authorization", "Bearer "+sharedPreferences.getString("access_token",""))
                                    .header("Accept", "application/vnd.web_app+json; version=1")
                                    .method(original.method(), original.body())
                                    .build();

        return chain.proceed(request);
    }
});

OkHttpClient client = duplicateTimeSessionClient.build();

final Retrofit duplicateTimeSession = new Retrofit.Builder()
                            .baseUrl(context.getString(R.string.URL))
                            .addConverterFactory(ScalarsConverterFactory.create())
                            .addConverterFactory(GsonConverterFactory.create())
                            .client(client)
                            .build();

duplicateService = duplicateTimeSession.create(DuplicateTimeSessionInterface.class);                   

Call<String> duplicateTimeSessionCall = duplicateService.duplicate(createJSON(pos),"api/time-sessions");

duplicateTimeSessionCall.enqueue(new Callback<String>() {
    @Override
    public void onResponse(Call<String> call, Response<String> response) {
        Log.i("time-session post",Integer.toString(response.code()));
    }

    @Override
    public void onFailure(Call<String> call, Throwable t) {}
});

接口

public interface DuplicateTimeSessionInterface {
    @POST
    Call<String>duplicate(@Body JSONObject jsonObject, @Url String url);
}

修改

同样的JSON可以在浏览器或iPhone上运行,而且一些类似的调用可以在Android上运行,这个调用只是不想通过。

0 个答案:

没有答案