2017-09-02 73 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