首页 > 解决方案 > 在 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();

         }
     });

 }

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

标签: androidretrofit

解决方案


使用

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

    }

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


推荐阅读