2017-09-02 27 views
1

我想第一次使用翻新并丢失简单的逻辑..请帮助我解决这个问题。带有查询参数的翻新网址

这是我的用户类

public class User { 

    private String name, email, password; 

    public User(){ 
    } 

    public User(String name, String email, String password){ 
     this.name = name; 
     this.email = email; 
     this.password = password; 
    } 

    public String getName() { 
     return name; 
    } 

    public String getEmail() { 
     return email; 
    } 

    public String getPassword() { 
     return password; 
    } 


    public void setName(String name) { 
     this.name = name; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 
} 

API接口

public interface MyApiEndpointInterface { 
     // Request method and URL specified in the annotation 
     // Callback for the parsed response is the last parameter 

     @GET("users?email={email}") 
     Call<User> getUser(@Query("email") String email); 
    } 

这是我得到的细节:

public void getUserDetails() 
{ 
    String email = inputEmail.getText().toString() 
    Call<User> call = apiService.getUser(email); 

    call.enqueue(new Callback<User>() { 
     @Override 
     public void onResponse(Call<User>call, Response<User> response) { 
      if(response.body()!=null) 
      { 
       Log.d("TAG", "Name: " + response.body().getName()); 
       Log.d("TAG", "Password: " + response.body().getPassword()); 
      } 
      else 
      { 
       Toast.makeText(getApplicationContext(), "User does not exist", Toast.LENGTH_SHORT).show(); 
       Log.d("TAG", "User details does not exist"); 
      } 
     } 

     @Override 
     public void onFailure(Call<User>call, Throwable t) { 
      Log.e("TAG", t.toString()); 
     } 
    }); 
} 

现在我的问题是我有网页API,是托管在服务器上,它看起来像:

http://www.somesite.com

,我们将根据电子邮件用户提供细节我试图用这个:

http://www.somesite.com/api/user?email= {EMAIL}

现在我该怎样设置的网址,API接口作为其返回null?

回答

2

在您的apiService中,您应该使用Builder创建一个Retrofit对象,并创建一个MyApiEndpointInterface接口的实例。在那里添加API的baseUrl。

它应该看起来是这样的:

Retrofit retrofit = new Retrofit.Builder() 
       .baseUrl("http://yourapibaseUrl.com/api/") 
       .build(); 

MyApiEndpointInterface apiInterface = retrofit.create(MyApiEndpointInterface.class); 

apiInterface是将用于使用重新安装对API的调用对象,将已经设定的baseUrl。

希望这会有所帮助.-

+0

啊傻......刚才我也看到它,因为它失踪了...谢谢哥们:) – coder