2016-03-14 52 views
3

我正在使用改造從http URL獲取數據。 我的接口類別:在改造中接收空體迴應

public interface SlotsAPI { 

    /*Retrofit get annotation with our URL 
     And our method that will return a Json Object 
    */ 
    @GET(url) 
    retrofit.Call<JSONObject> getSlots(); 
} 

我的請求方法。

public void getResponse(){ 

    Retrofit retrofit = new Retrofit.Builder() 
        .baseUrl(URL) 
        .addConverterFactory(GsonConverterFactory.create()) 
        .build(); 

    //Creating an object of our api interface 
    SlotsAPI api = retrofit.create(SlotsAPI.class); 
    retrofit.Call<JSONObject> callback = api.getSlots(); 
    callback.enqueue(new Callback<JSONObject>() { 
    @Override 
    public void onResponse(Response<JSONObject> response) { 
     if (response != null) { 
      Log.d("OnResponse", response.body().toString()); 
     } 
    } 

    @Override 
    public void onFailure(Throwable t) { 
     t.printStackTrace(); 
    } 
    }); 
} 

在響應中,我收到一個空的body.And服務器響應200 OK。

D/OnResponse: {} 

但是當我在瀏覽器中打開URL時,我在屏幕上獲取JSONObject。

+0

你可以提供網址嗎? –

+0

對不起,我不能。但它是一個普通的http鏈接。 – Viking93

+0

我建議你使用諸如Fiddler或Charles之類的工具,並通過它們代理這兩種流量。然後,您可以比較請求中的差異並獲得想法。 也許是因爲UserAgent字符串。 –

回答

6

你應該嘗試像這樣的陣列GSON轉換器將處理它....

public interface SlotsAPI { 

/*Retrofit get annotation with our URL 
    And our method that will return a Json Object 
*/ 
@GET(url) 
Call<JsonElement> getSlots(); 
} 

在請求方法

retrofit.Call<JsonElement> callback = api.getSlots(); 
callback.enqueue(new Callback<JsonElement>() { 
@Override 
public void onResponse(Response<JsonElement> response) { 
    if (response != null) { 
     Log.d("OnResponse", response.body().toString()); 
    } 
} 
0

我想你不理解改造filosofy。 了正確的接口應該是:

public interface SlotsAPI { 

    /*Retrofit get annotation with our URL 
     And our method that will return a Json Object 
    */ 
    @GET(url) 
    JSONObject getSlots(); 
} 

當你調用getSlots方法,改造將自動進行HTTP請求和返回的JSONObject。 您將需要從主線程中執行此操作。

+0

我認爲這個調用不會是異步的。但在我的情況下,我需要異步調用服務器。所以我使用回調方法。謝謝。 – Viking93

0

確保@Get的URL是相對路徑

  • @Base網址:始終/

  • @url結束:不要啓動/

例如:

String URL = http://api.co/base/ ; 

@GET("webservice/syncdown") 
    JSONObject getSlots(); 

您可能會收到插槽列表。如果您發送的JSON

@GET(url) 
retrofit.Call<List<Slot>> getSlots(); 
+0

我會試試這個,讓你知道這是否有效。謝謝。 – Viking93

+0

不,它仍然給出相同的空身。 – Viking93

+0

我有一個問題,你確定從你正在設置一個轉換器工廠GsonConverterFactory設置的對象的類型,並且響應正文也是JSONObject? 查看我的更新瞭解我的問題 –

0

您正在使用翻新2或1?版本2仍處於測試階段。 如果您正在使用的版本1.使用這樣的:

public interface SlotsAPI { 

    /*Retrofit get annotation with our URL 
     And our method that will return a Json Object 
    */ 
    @GET(url) 
    void getSlots(Callback<JsonElement> callback); 
} 

有了這個電話將是異步的。

+0

我正在使用Retrofit 2.我知道2處於測試階段。那麼你確定它是2.0的錯誤還是我做錯了什麼? – Viking93

0

這裏同樣的問題,answer from curiousMind救了我一天。
更多關於同一主題:如果你需要從一對使用得到的值:

String value = response.body().getAsJsonObject().get("pair_name").getAsString(); 
4

請檢查您的JSONObject。如果你想在json中獲得響應,你必須定義一個響應類型JsonObject而不是JSONObject,否則明確指定接口中的pojo類。

+0

他們的拼寫是關閉'JsonObject'和'JSONObject'。我們應該注意一個是'com.google.gson.JsonObject',它由'gson'轉換器工廠使用,另外一個'org.json.JSONObject',我們必須使用第一個'com.google.gson.JsonObject' 。 – VSB