Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

retrofit 400 Bad Request

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) {



                }
            });
}

Interface class

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

    }

getting 400 Bad Request.

Error log

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

Any help would be really appreciated.

like image 702
Murali Ganesan Avatar asked Dec 07 '15 12:12

Murali Ganesan


Video Answer


2 Answers

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();

add dependencies to gradle

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

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

Example:

 {"statusCode":400,"message":"Some parameters are missing"}
 D/Retrofit: <--- END HTTP (76-byte body)
like image 124
Emma Avatar answered Sep 23 '22 16:09

Emma


I solve my problem after sending data in Map. its work for me.

     Map<String,String> hashMap=new HashMap<>();
    hashMap.put("client_id","2");
    hashMap.put("client_secret",Constant.CLIENT_SECRET);
    hashMap.put("grant_type",Constant.GRANT_TYPE);
    hashMap.put("username",et_user.getText.toString();
    hashMap.put("password",etpassword.getText().toString());
    Call<LoginDataModel> call=apiInterface.login(hashMap);

And in ApiInterface Just i have add a method like this

 @FormUrlEncoded
@POST("oauth/token")
Call<LoginDataModel> login(@FieldMap Map<String, String> options);
like image 42
sanjeev kumar Avatar answered Sep 23 '22 16:09

sanjeev kumar