在获取方法中返回空值的改造响应



>我有一个这样的网址

http://182.72.198.001:8080/MyLogin/login/xxxxxx/yyyyyy
userName : xxxxxx
password : yyyyyy

我正在使用改造来获取上面的 url 响应,但它返回空,我的改造类是

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

如果我在此代码中做错了什么,我该如何解决这个问题?

http://182.72.198.001:8080/MyLogin/login/xxxxxx/yyyyyy

如果您必须形成上述网址,则必须在网址路径中发送用户名和密码。因此,您的改造界面应如下所示:

public interface MyInterface {
@GET("login/{userName}/{password}")
Call<ResponseBody> LoginValidation(@Path("userName") String username, 
@Path("password") String password);
}

使用

@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);
}

使用此方法,您将获得所需的确切结果。

最新更新