改造 400 错误请求 [英] retrofit 400 Bad Request

查看:77
本文介绍了改造 400 错误请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用于网络调用的函数.

This is the function which i used for the network calls.

private void getSample() {


    Log.d("", "getSample : ");
    OkHttpClient client = new OkHttpClient();

    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    client.interceptors().add(interceptor);

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(NetworkCalls.BASE_URL)
            .client(client)

            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    NetworkCalls.CustomerApi customerApi = retrofit.create(NetworkCalls.CustomerApi.class);

    Log.e("customerApi  Created", "customerApi object Created : ");

    customerApi.getCategories("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjgsImlzcyI6Imh0dHA6XC9cLzE5Mi4xNjguMS42MDo4ODg4XC9DQ1YzXC9wdWJsaWNcL2FwaVwvbG9naW4iLCJpYXQiOjE0NDk0ODg5NDMsImV4cCI6MTQ0OTQ5MjU0MywibmJmIjoxNDQ5NDg4OTQzLCJqdGkiOiI0ODY4ZmNmZDk4OTIxZGQwYjA2ZDZlN2EyYjRjZGMxMSJ9.7c60LRQMFCtqprCbbULgR5xG-FxwXnRUZPuWeMJXcNE")
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Subscriber<Response<DataCategoryList>>() {
                @Override
                public void onCompleted() {

                    Log.e("onCompleted", "onCompleted : ");

                }

                @Override
                public void onError(Throwable e) {
                    Log.e("onError", "onError : " + e.getMessage());
                    e.printStackTrace();

                    // network errors, e. g. UnknownHostException, will end up here
                }

                @Override
                public void onNext(Response<DataCategoryList> startupResponseResponse) {



                }
            });
}

接口类

public interface CustomerApi {
    @Headers({
            "Accept: application/json"
    })
    @FormUrlEncoded
    @POST("categories")
    Observable<Response<DataCategoryList>> getCategories(@Field("token") String token);

    }

收到 400 个错误请求.

getting 400 Bad Request.

错误日志

  <-- HTTP/1.1 400 Bad Request (207ms)
  12-07 17:48:56.408 21024-21067/com.cc.customer D/OkHttp: Date: Mon, 07 Dec 2015 12:19:22 GMT
  12-07 17:48:56.408 21024-21067/com.cc.customer D/OkHttp: Server: Apache/2.2.29 (Unix) mod_wsgi/3.5 Python/2.7.10 PHP/5.6.10 mod_ssl/2.2.29 OpenSSL/0.9.8zg DAV/2 mod_fastcgi/2.4.6 mod_perl/2.0.9 Perl/v5.22.0
  12-07 17:48:56.408 21024-21067/com.cc.customer D/OkHttp: X-Powered-By: PHP/5.6.10
  12-07 17:48:56.408 21024-21067/com.cc.customer D/OkHttp: Cache-Control: no-cache
  12-07 17:48:56.408 21024-21067/com.cc.customer D/OkHttp: Set-Cookie: XSRF-TOKEN=5Uqd6WSjbalLcvX3o9RLDY1bGt69ktNoiLZZOahP; expires=Mon, 07-Dec-2015 14:19:22 GMT; Max-Age=7200; path=/
  12-07 17:48:56.408 21024-21067/com.cc.customer D/OkHttp: Set-Cookie: laravel_session=882b5bdedd5dac0fcbfb88706406e3cc0acf91f6; expires=Mon, 07-Dec-2015 14:19:22 GMT; Max-Age=7200; path=/; httponly
  12-07 17:48:56.408 21024-21067/com.cc.customer D/OkHttp: Content-Length: 30
  12-07 17:48:56.408 21024-21067/com.cc.customer D/OkHttp: Connection: close
  12-07 17:48:56.408 21024-21067/com.cc.customer D/OkHttp: Content-Type: application/json
  12-07 17:48:56.408 21024-21067/com.cc.customer D/OkHttp: OkHttp-Selected-Protocol: http/1.1
  12-07 17:48:56.408 21024-21067/com.cc.customer D/OkHttp: OkHttp-Sent-Millis: 1449490736233
  12-07 17:48:56.408 21024-21067/com.cc.customer D/OkHttp: OkHttp-Received-Millis: 1449490736410
  12-07 17:48:56.408 21024-21067/com.cc.customer D/OkHttp: <-- END HTTP (30-byte body)

任何帮助将不胜感激.

推荐答案

400 听起来像是您从服务器获得的特定错误代码.您可以通过在 Retrofit 上添加 loglevel 来尝试调试.

400 sounds like the specific error code that you are getting from your server. You can try debugging by adding loglevel on Retrofit.

OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
clientBuilder.addInterceptor(loggingInterceptor);

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(NetworkCalls.BASE_URL)
        .client(clientBuilder.build())
        .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
        .addConverterFactory(GsonConverterFactory.create())
        .build();

向 gradle 添加依赖

add dependencies to gradle

compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'

使用日志级别,您可能会看到一些错误 json 从您的服务器返回.

With the loglevel, you can possibly see some error json back from your server.

示例:

 {"statusCode":400,"message":"Some parameters are missing"}
 D/Retrofit: <--- END HTTP (76-byte body)

这篇关于改造 400 错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆