当前位置:  开发笔记 > 编程语言 > 正文

改造400 Bad Request

如何解决《改造400BadRequest》经验,为你挑选了1个好方法。

这是我用于网络呼叫的功能.

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>() {
                @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 startupResponseResponse) {



                }
            });
}

接口类

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

    }

获得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)

任何帮助将非常感激.



1> Emma..:

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

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

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

使用loglevel,您可能会从服务器上看到一些错误json.

例:

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

推荐阅读
黄晓敏3023
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有