Getting Bad Request error don't know why help me to resolve it i now their is small error but not able to understand where is problem
Api Interface
@POST("token")
Call<LoginResponse> loginresponse(@Body RequestBody login);
Api CLient
public class ApiClient {
private static final String BASE_URL = "http://falkentyre.in/";
private static Retrofit retrofit;
private static Retrofit retrofit1;
private ApiClient(Context context) {
//Add gson to retrofit
Gson gson = new GsonBuilder()
.setLenient()
.create();
// Add Ok http client to retrofit
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(chain -> {
Request original = chain.request();
Request.Builder requestBuilder = original.newBuilder();
return chain.proceed(requestBuilder.build());
});
OkHttpClient mOkHttpClient = builder.build();
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(mOkHttpClient)
.build();
}
public static Retrofit getClient() {
if (retrofit1 == null) {
OkHttpClient client = new OkHttpClient().newBuilder()
.readTimeout(1000, TimeUnit.SECONDS)
.connectTimeout(1000, TimeUnit.SECONDS)
.build();
Gson gson = new GsonBuilder()
.setLenient()
.create();
retrofit1 = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.client(client)
.build();
}
return retrofit1;
}
public <S> S createService(Class<S> serviceClass) {
return retrofit.create(serviceClass);
}
}
/////////////////////////////////////////////////////
public void take_Login(String no,String pass) {
showProgressBar();
String login = String.format("grant_type=password&username=%s&password=%s&device=android¬ificationtoken=hgh",
no, pass);
RequestBody body = RequestBody.create(MediaType.parse("text/plain"), login);
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<LoginResponse> userCall = apiInterface.loginresponse(body);
userCall.enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
if (response.isSuccessful()) {
hideProgressBar();
Toast.makeText(MainActivity.this,response.body().getAccess_token(),Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this,response.message(),Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<LoginResponse> call, Throwable t) {
Toast.makeText(MainActivity.this,t.getMessage(),Toast.LENGTH_SHORT).show();
}
});
}
Try This,
In ApiInterface
@FormUrlEncoded
@POST("token")
Call<LoginResponse> loginresponse(@FieldMap HashMap<String, String> login);
For call use like this,
HashMap<String,String> map = new HashMap<>();
map.put("grant_type","password");
map.put("username","username want to pass");
map.put("password","password want to pass");
map.put("device","android");
map.put("notificationtoken","hgh");
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<LoginResponse> userCall = apiInterface.loginresponse(map);
For
GET
method you can try
@GET("token")
Call<LoginResponce> loginresponse(@Query("grant_type") String grant_type, @Query("username") String username, @Query("password") String password,@Query("device") String device, @Query("notificationtoken") String notificationtoken);
You can call this it like this
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<LoginResponse> userCall = apiInterface.loginresponse("grandType you want","username you want","password you want","android","hgh");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.