遇到错误的请求错误,不知道为什么能帮助我解决它 我现在他们的错误很小,但无法理解问题所在
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();
}
});
}
答案 0 :(得分:0)
尝试一下
在ApiInterface中
@FormUrlEncoded
@POST("token")
Call<LoginResponse> loginresponse(@FieldMap HashMap<String, String> login);
对于这样的通话用途,
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);
对于
GET
方法,您可以尝试
@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);
您可以这样称呼它
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<LoginResponse> userCall = apiInterface.loginresponse("grandType you want","username you want","password you want","android","hgh");