簡體   English   中英

改裝POST包含空主體

[英]Retrofit POST contains null body

我正在設置一個帶有改進功能的api客戶端,到目前為止,GET都可以正常工作,但是我正在嘗試使用POST創建一個新對象,並且請求正文僅包含字符串,而不是作為json發送的對象“空值”:

---> HTTP POST http://myapiurl
Content-Type: application/json; charset=UTF-8
Content-Length: 4
null
---> END HTTP (4-byte body)

這是我嘗試調用的方法:

@POST("/Monsters/")
Response new_monster(@Body Monster mon);

這是我的稱呼方式:

@Test
public void testNew_monster() throws Exception {
    //create a new monster object, and pass it to the function,
    //then query and verify it's in the results?
    Monster newmon = new Monster() {
        {
            name = "deleteme";
            description = "created by junit test testNew_monster";
            image_url = "http://i.imgur.com/";
            created_by = "";
            encoded_key = "";
        }
    };

    Response r = client.new_monster(newmon);
    assertEquals(201, r.getStatus());
    //sleep a couple seconds here?

    List<Monster> monsterList = client.monsters();
    assertTrue(monsterList.contains(newmon));
}

我猜測當使用GSON將對象序列化為json時出現了問題,但是在序列化過程中我看不到調試器有任何幫助...

我正在使用GSON版本2.3.1

編輯:這是我如何構建RestAdapter和客戶端的方法:

static MonSpottingApi GetClient(boolean dbg)
{
    RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint(API_URL)
            .build();

    if (dbg) restAdapter.setLogLevel(RestAdapter.LogLevel.FULL);

    MonSpottingApi client = restAdapter.create(MonSpottingApi.class);

    return client;
}

在測試用例類中:

MonSightingClient.MonSpottingApi client;

@Before
public void setUp() throws Exception {
    client = MonSightingClient.GetClient(true);
}

我懷疑根本原因是Gson,因此我開始進行非常簡單的測試,並嘗試使用toJson()獲取對象正確序列化的對象。 我想我在Gson中發現了一個錯誤,如果使用雙括號語法初始化對象,該錯誤將失敗:

使用此處找到的示例類

public class GitHubTest {

    //fails
    @Test
    public void testGson1() throws Exception {
        GitHubClient.Contributor contrib = new GitHubClient.Contributor() {
            {
                login = "someguy";
                contributions = 99;
            }
        };
        Gson gson = new Gson();
        String json = gson.toJson(contrib);
        System.out.println("contents of json string: " + json);
        assertNotEquals(json, "null");
    }

    //passes
    @Test
    public void testGson2() throws Exception {
        GitHubClient.Contributor contrib = new GitHubClient.Contributor();
        contrib.contributions = 99;
        contrib.login = "someguy";
        Gson gson = new Gson();
        String json = gson.toJson(contrib);
        System.out.println("contents of json string: " + json);
        assertNotEquals(json, "null");
    }
}

這是Gson中的錯誤嗎? 還是有一些奇怪的Java細微原因導致這種情況發生? (Java不是我最強的語言)。

我遇到了同樣的問題,而且看起來Gson對要轉換的對象的雙括號初始化不滿意。

替換雙括號初始化...

 AuthenticationRequest req = new AuthenticationRequest() {{
     setName("myName"); 
     setPassword("myPass");
 }}

與...

AuthenticationRequest req = new AuthenticationRequest();
req.setUserId("myName");
req.setPassword("myPass");

...成功了

您必須將接口類傳遞給Restadapter create()方法。 假設您的接口類是INewService (在其中聲明了new_monster ),那么GetClient應該如下所示:

public static INewService GetClient(boolean dbg){

    RestAdapter restAdapter = new RestAdapter.Builder().
    .setEndpoint(API_URL).
    .setClient(new OkClient(new OkHttpClient()))
    .build();

    if (dbg) restAdapter.setLogLevel(RestAdapter.LogLevel.FULL);

    return restAdapter.create(INewService.class);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM