Pracuję nad kodem serwera, w którym klient wysyła żądania w formie JSON. Mój problem polega na tym, że istnieje wiele możliwych żądań, z których wszystkie różnią się małymi szczegółami implementacji. Dlatego uważa się, że korzystać z interfejsu żądania, zdefiniowana jako:Używanie Gson z typami interfejsów
public interface Request {
Response process ();
}
Stamtąd I wdrożone interfejsu w klasie o nazwie LoginRequest
jak pokazano:
public class LoginRequest implements Request {
private String type = "LOGIN";
private String username;
private String password;
public LoginRequest(String username, String password) {
this.username = username;
this.password = password;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
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;
}
/**
* This method is what actually runs the login process, returning an
* appropriate response depending on the outcome of the process.
*/
@Override
public Response process() {
// TODO: Authenticate the user - Does username/password combo exist
// TODO: If the user details are ok, create the Player and add to list of available players
// TODO: Return a response indicating success or failure of the authentication
return null;
}
@Override
public String toString() {
return "LoginRequest [type=" + type + ", username=" + username
+ ", password=" + password + "]";
}
}
Aby pracować z JSON, stworzyłem GsonBuilder
przykład i zarejestrował InstanceCreator
jak pokazano poniżej:
public class LoginRequestCreator implements InstanceCreator<LoginRequest> {
@Override
public LoginRequest createInstance(Type arg0) {
return new LoginRequest("username", "password");
}
}
które następnie stosuje się, jak pokazano na opis poniżej:
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(LoginRequest.class, new LoginRequestCreator());
Gson parser = builder.create();
Request request = parser.fromJson(completeInput, LoginRequest.class);
System.out.println(request);
i otrzymuję oczekiwany wynik.
Rzeczą, którą chcę zrobić, jest zastąpienie linii Request request = parser.fromJson(completeInput, LoginRequest.class);
czymś podobnym do Request request = parser.fromJson(completeInput, Request.class);
, ale to nie zadziała, ponieważ Request
jest interfejsem.
Chcę, aby mój Gson
zwrócił odpowiedni typ żądania w zależności od otrzymanego JSON.
Przykładem JSON I przekazywane do serwera znajduje się poniżej:
{
"type":"LOGIN",
"username":"someuser",
"password":"somepass"
}
Powtórzmy, szukam sposób do analizowania żądań (w JSON) z klientami i powrotu obiektów klas wykonawczego Request
interfejs
Czy możesz podać inne przykłady różnych odpowiedzi JSON, które możesz uzyskać z serwera? Bo jeśli nie masz wielu i bardzo różnych możliwości, jest coś, co możesz zrobić łatwo ... – MikO
Dzięki @MiKO za twoje wejście. Inne prawdopodobne żądania to 'PlayRequest',' LogoutRequest', 'GetPlayersRequest',' JoinGameRequest', 'StartGameRequest' itd ... – fredmanglis
Mam na myśli, jeśli możesz podać przykład żądania JSON dla co najmniej jednego z tych innych typów żądań . Mam na myśli, dla twojego 'LoginRequest' masz pola:' type', 'username' i' password', a co z innymi żądaniami? Jak wyglądają? – MikO