2017-08-21 12 views
0

私はちょうどretrofitライブラリの助けを借りてユーザーの詳細を取得しようとします、それはnullを返しますが、事前に同じ入力でクライアントを休んで、それは働いています正しく。アンドロイドでヌルを返すRetrofit

マイApiClient

Public class Apiclient{ 
    public static final String BASE_URL = Webconstants.COMMON_URL; 
    private static Retrofit retrofit = null; 

    public static Retrofit getclient() { 
    if (retrofit== null){ 
     retrofit = new Retrofit.Builder() 
       .baseUrl(BASE_URL) 
       .addConverterFactory(GsonConverterFactory.create()) 
       .build(); 
    } 
    return retrofit; 
    } 
} 

マイApiInterface

@Headers({"Content-Type: application/json"}) 
@POST("user/GetUserDetails") 
Call<LoginResponce> Login(@Body JSONObject data); 

そして

private void loginTask(JSONObject input){ 
    ApiInterfaces apiService = Apiclient.getClient().create(ApiInterfaces.class); 
    Call<LoginResponce> login_call=apiService.Login(input); 
    login_call.enqueue(new Callback<LoginResponce>(){ 
     @Override 
     public void onResponce(Call<LoginResponce> call, Responce<LoginResponce> responce) { 
      Error error = responce.body().getError; 
      if(error.getError_data() == 0){ 
       User user = responce.body.getUser(); 
      }else{ 
      } 
     } 
     @Override 
     public void onFailure(Call<LoginResponce> call, Throwable t){ 
     } 
    }); 
    } 
} 

LoginResponceモデル

ようなものを呼び出します
public class LoginResponce { 

    @SerializedName("error") 
    private Error error; 

    @SerializedName("users") 
    private User user; 

    public Error getError(){ return error;} 
    public User getUser(){ return user;} 
} 

エラーとユーザーは同じ種類のモデルです。高度なレストクライアントでは、Content-Typeapplication/jsonです。

答えて

0

JsonObjectを渡すべきではありません。代わりにモデルオブジェクトを渡すと結果が得られます。

あなたのモデルオブジェクトはjsonに変換されて送信されます。したがって、直接JsonObjectを渡すことはできません。

たとえば、ユーザー名とパスワードをAPIに送信する場合は、あなたはこのように送る必要があります。

あなたのモデル:

public UserModel{ 
    private String userName; 
    private String password; 

public String getUserName() { 
    return userName; 
} 

public void setUserName(String userName) { 
    this.userName = userName; 
} 

public String getPassword() { 
    return password; 
} 

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

あなたはデータを設定し、このようにAPIを呼び出す必要があります。

UserModel model = new UserModel(); 
model.setUserName("username"); 
model.setPassword("password"); 

@Headers({"Content-Type: application/json"}) 
@POST("user/GetUserDetails") 
Call<LoginResponce> Login(@Body UserModel data); 

はそれが役に立てば幸い:)

関連する問題