改造响应在get方法中返回null

时间:2018-10-11 07:35:26

标签: android retrofit

我有这样的网址

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

userName : xxxxxx
password : yyyyyy

我正在使用改造来获得上述网址响应,但我的改造类返回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();

         }
     });

 }

我该如何解决此代码中的任何错误?

2 个答案:

答案 0 :(得分:1)

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

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

public interface MyInterface {

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

}

答案 1 :(得分:1)

使用

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

    }

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