2017-07-23 7 views
0

Ich möchte ein JSON-Objekt an Post-Anfrage senden.Retrofit JSON OBJECT Beitrag

Die Json haben die nächste Struktur:

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

Das funktioniert schön in POSTMAN aber ich weiß nicht, wie ich habe den Schlüssel in der Nachrüstung wie in POSTMAN zu definieren.

enter image description here

In diesem Moment habe ich eine @Body wie folgt aus:

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; 
} 

}

Aber ich weiß wirklich nicht, wo ich den Schlüssel zu definieren. Dann versuche ich, die POST-Anforderung wie folgt zu nennen: enter image description here

Antwort

2

Verwenden @Field("json") statt @Body in Ihrem Endpunkt Definition:

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

Außerdem müssen Sie einen Konverter verwenden, um Objekte zu konvertieren. Hier ist ein Beispiel für GSON. Sie müssen im Wesentlichen einen benutzerdefinierten Wrapper für den Standardwert GsonConverterFactory erstellen, da er die stringConverter(...)-Methode, die mit @Field annotierte Werte und solche in Zeichenfolgen konvertiert, nicht implementiert.

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); 
     } 
    } 
} 

Dann, wenn Sie die Retrofit-Instanz erstellen, benutzen Sie einfach diesen Adapter:

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

funktioniert gut, danke. –

+0

Jetzt kann ich ausführen, aber immer zurückgeben kann nicht authentifizieren, checkin es scheint, dass mein Objekt ist null, aber verstehe nicht warum. –

+0

Ich weiß nicht, welche Art von Konverterfabrik Sie verwenden, aber es scheint, als würde 'GsonConverterFactory' den' stringConverter' nicht implementieren. Ich werde die Antwort aktualisieren, um einen neuen Konverter für GSON zu erstellen. –

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

Haben Sie INTERNET-Berechtigung in Ihrem Manifest? –

+0

Habe es. Ich habe einen anderen Fehler. Funktioniert nur, wenn ich es mit einem LoginRequest-Objekt gemacht habe. Danke für die Antwort –