2017-07-23 2 views
0

Je souhaite envoyer un objet json pour publier une demande.Retrofit JSON OBJECT Message

JSON ont la prochaine structure:

{ "email:"[email protected]", 
    "password": "test", 
    "hash": "true" 
} 

Cela fonctionne bien dans POSTMAN mais je ne sais pas comment je dois définir la clé dans la modernisation comme dans POSTMAN. A ce moment

enter image description here

, je créé un @Body comme ceci:

public class LoginRequest { 


private String email; 
private String password; 
private String gethash; 

public LoginRequest(String email, String password, String hash) { 
    this.email = email; 
    this.password = password; 
    this.gethash = hash; 
} 

}

Mais je ne sais vraiment pas où je dois définir la clé. Ensuite, je suis en train d'appeler la requête POST comme ceci: enter image description here

Répondre

2

Utilisation @Field("json") au lieu de @Body dans la définition du point final:

@POST("/login") 
public Observable<DataLogin> getLogin(@Field("json") LoginRequest loginRequest); 

En outre, vous devez utiliser un convertisseur pour convertir des objets. Voici un exemple pour GSON. Vous aurez essentiellement besoin de créer un wrapper personnalisé pour le GsonConverterFactory par défaut car il n'implémente pas la méthode stringConverter(...) qui convertit les valeurs annotées avec @Field et ainsi de suite en chaînes.

public class GsonStringConverterFactoryWrapper extends Converter.Factory { 
    private GsonConverterFactory converterFactory; 

    public static GsonStringConverterFactoryWrapper create() { 
     return create(new Gson()); 
    } 

    @SuppressWarnings("ConstantConditions") 
    public static GsonStringConverterFactoryWrapper create(Gson gson) { 
     if (gson == null) throw new NullPointerException("gson == null"); 
     return new GsonStringConverterFactoryWrapper(gson); 
    } 

    private final Gson gson; 

    private GsonStringConverterFactoryWrapper(Gson gson) { 
     this.gson = gson; 
     converterFactory = GsonConverterFactory.create(gson); 
    } 

    @Override 
    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, 
                  Retrofit retrofit) { 
     return converterFactory.responseBodyConverter(type, annotations, retrofit); 
    } 

    @Override 
    public Converter<?, RequestBody> requestBodyConverter(Type type, 
                  Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) { 
     return converterFactory.requestBodyConverter(type, parameterAnnotations, methodAnnotations, retrofit); 
    } 

    @Nullable 
    @Override 
    public Converter<?, String> stringConverter(Type type, Annotation[] annotations, Retrofit retrofit) { 
     return new GsonStringConverter<>(gson); 
    } 

    public static class GsonStringConverter<T> implements Converter<T, String> { 
     private final Gson gson; 

     GsonStringConverter(Gson gson) { 
      this.gson = gson; 
     } 

     @Override 
     public String convert(@NonNull T value) throws IOException { 
      return gson.toJson(value); 
     } 
    } 
} 

Ensuite, lorsque vous créez l'instance Retrofit, il suffit d'utiliser cet adaptateur:

new Retrofit.Builder() 
.addConverterFactory(GsonStringConverterFactoryWrapper.create(gson)) // if you don't have a custom GSON instance, just call .create() 
// [...] other settings 
.build(); 
+0

fonctionne très bien, merci. –

+0

Maintenant, je peux exécuter mais toujours retourner ne peut pas authentifier, checkin, il semble que mon objet est nul, mais je ne comprends pas pourquoi. –

+0

Je ne sais pas quel type d'usine de conversion vous utilisez, mais il semble que 'GsonConverterFactory' n'implémente pas le' stringConverter'. Je mettrai à jour la réponse pour créer un nouveau convertisseur pour GSON. –

0
@FormUrlEncoded 
@POST("/login") 
public Observable<DataLogin> getLogin(@Field("json") String json); 
+0

Avez-vous l'autorisation INTERNET dans votre manifeste? –

+0

Avoir. J'ai une autre erreur. Ne fonctionne que si je l'ai fait avec un objet LoginRequest. Merci pour la réponse –