2017-07-23 154 views
0

我想發送一個json對象來發布請求。Retrofit JSON對象Post

JSON的有一個結構:

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

這個工作在郵差不錯,但我不知道我是如何定義的改裝重點像郵差。

enter image description here

在這一刻,我創造了這樣一個@Body:

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

}

但我真的不知道我必須定義的關鍵。 然後,我想這樣調用POST請求: enter image description here

回答

2

使用@Field("json")而不是@Body在您的端點定義:

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

而且,你必須使用轉換器轉換的對象。以下是GSON的示例。您基本上需要爲默認的GsonConverterFactory創建自定義包裝,因爲它沒有實現stringConverter(...)方法,該方法將用@Field等註釋的值轉換爲字符串。

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

然後,當你創建的改造實例,只需使用該適配器:

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

工作很好,謝謝。 –

+0

現在我可以執行但總是返回無法進行身份驗證,檢查它,似乎我的對象爲空,但不明白爲什麼。 –

+0

我不知道你使用了什麼樣的轉換器工廠,但似乎'GsonConverterFactory'沒有實現'stringConverter'。我會更新答案,爲GSON創建一個新的轉換器。 –

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

您的清單中是否有INTERNET權限? –

+0

有。我有另一個錯誤。只有在我使用LoginRequest對象時纔有效。 感謝您的回答 –