android - 收到错误请求错误
问题描述
出现错误请求错误不知道为什么要帮我解决它我现在是小错误但无法理解问题出在哪里
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();
}
});
}
解决方案
尝试这个,
在 API 接口中
@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");
推荐阅读
- rust - 如何在 Rust 中编写自己的可链接迭代器函数?
- c# - 有或没有等待调用的 C# 异步方法调用给出相同的结果?
- python - Opencv - 从游戏图像中提取数据
- amazon-web-services - 在 AWS 中管理客户端
- vba - (VBA-PPT) Redim 错误 9?索引超出范围
- sql - 使用非标准编号枚举具有父子关系的表
- android - 如何将 html 描述(来自 API 响应)呈现为 Android EditText 输入字段中的简单文本?
- c++ - std::String 调整大小不会改变地址
- reactjs - 如何修复所有 .js 文件中的 history.push?
- android - Java(Android)中的RSA密钥签名和验证