android - 在 get 方法中返回 null 的改造响应
问题描述
我有一个这样的网址
http://182.72.198.001:8080/MyLogin/login/xxxxxx/yyyyyy
userName : xxxxxx
password : yyyyyy
我正在使用改造来获得上述 url 响应,但它返回 null 我的改造类是
public class ApiClient {
public static Retrofit retrofit = null;
public static Retrofit getApiCLient() {
if (retrofit == null) {
retrofit = new Retrofit.Builder().baseUrl("http://182.72.198.001:8080/MyLogin/").addConverterFactory
(GsonConverterFactory.create()).build();
}
return retrofit;
}
}
myInterface 类是
public interface MyInterface {
@GET("login/")
Call<ResponseBody> LoginValidation(@Query("userName") String username, @Query("password") String password);
}
我的主要课程是
MyInterface loginInterface;
loginInterface = ApiClient.getApiCLient().create(MyInterface.class);
private void LoginUser()
{
final String usedrname = username1.getText().toString();
final String password = password1.getText().toString();
Call<ResponseBody> call = loginInterface.LoginValidation(usedrname,password);
Log.i("Tag","Interface" + usedrname+password);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Log.i("Tag","Respose" + response.body()); // this body returns null
Toast.makeText(getApplicationContext(), "Thank You!", Toast.LENGTH_SHORT).show();
editor.putString("username",usedrname);
editor.putString("password",password);
editor.putBoolean("isLoginKey",true);
editor.commit();
Intent i=new Intent(MainActivity.this,Navigation.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(getApplicationContext(),"Username or password Mismatch",Toast.LENGTH_LONG).show();
}
});
}
我该如何解决这个问题我在这段代码中做错了什么?
解决方案
使用
@GET("login/")
Call<ResponseBody> LoginValidation(@Query("userName") String username, @Query("password") String password);
您的网址变为: http: //182.72.198.001 :8080/MyLogin/login/?userName=xxxxxx&password=yyyyyy
要根据需要准确调用,请使用它(由@Avijit Karmakar回答)
public interface MyInterface {
@GET("login/{userName}/{password}")
Call<ResponseBody> LoginValidation(@Path("userName") String username,
@Path("password") String password);
}
使用此方法,您将获得所需的确切结果。
推荐阅读
- php - 尝试在 Windows 10 上安装 composer 时出错
- java - Java中的ArrayList操作
- c# - 我们如何到达一个实例并为其赋值?
- c# - SQLite 数据库与在线数据库 C# UWP
- javascript - Angular 5 绑定选择/选项与物化 css
- powershell - 使用特殊字符重命名 CSV 文件中的列标题
- mysql - 如何从 Mysql 中获取最新的行
- javascript - 名称冲突时如何使用 JavaScript 解构赋值
- javascript - 在角度更改路线时调用服务
- traefik - 为 /admin 路径前缀添加基本身份验证