2016-07-28 46 views
-1

我打算做一個公共服務類與使用改造的,改造動態ResponseBody

@GET 
Call<ResponseBody> makeGetRequest(@Url String url); 

@POST 
Call<ResponseBody> makePostRequest(@Url String url, @Body RequestBody parameters); 

在這段代碼我需要傳遞(ResponseBody)作爲動態JSON POJO類的名稱,如LoginRes

說,例如,

Call<LoginRes> // But this class will be dynamic 

我會把ResponseBody但ResponseBody不知道我想喜歡哪一類。

爲什麼我想這是因爲,結果後

gson.fromJson(response, LoginRes.class); 

所以,從改造得到結果後,我們又需要轉換爲gson.fromJson。

,所以我想通過動態響應改造,使其按照我的POJO類將響應,

我知道這是工作正常,當我通過LoginRes代替ResponseBody因爲我已經告訴響應我們需要LoginRes中的響應。

所以,如果我通過

Call<LoginRes> // if i pass this way its working fine no need to convert my response i can access my all properties from that LoginRes class directly. 

這是我的例子來調用Web服務。

Call<ResponseBody> call = apiService.makePostRequest("/Buyer/LoginApp", requestBody); 

這就是我稱之爲服務的方式。

讓我知道如果我不清楚解釋我的問題。

等待一些很好的迴應和建議。

感謝 馬達夫

+0

所以你想要有相同的功能,可以將JSON響應轉換爲各種可能完全不相關的類? – NitroNbg

+0

實際上,如果我可以通過ResponseBody 動態或不同的方式..我需要的建議。 –

回答

0

既然有這麼多的用戶給予DOWN投票,我已經通過我自己解決了這個 問題,

處理GET ,POST或其他方法像這樣,

if (methodType.equalsIgnoreCase(CommonConfig.WsMethodType.GET)) { 
       apicall = getClient(CommonConfig.WsPrefix).create(ApiInterface.class).makeGetRequest(url + CommonConfig.getQueryString(new Gson().toJson(requestBody)), getAllHeader); 
} else if (methodType.equalsIgnoreCase(CommonConfig.WsMethodType.POST)) { 
       apicall = getClient(CommonConfig.WsPrefix).create(ApiInterface.class).makePostRequest(url, RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(requestBody)), getAllHeader); 
} 

處理響應像這樣。

apicall.enqueue(new Callback<ResponseBody>() { 
           @Override 
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { 
} 

@Override 
public void onFailure(Call<ResponseBody> call, Throwable t) { 

} 
} 

改造代碼

private Retrofit getClient(String WsPrefix) { 
     //TODO 60 to 30 second at everywhere 
     OkHttpClient okHttpClient = new OkHttpClient().newBuilder() 
       .connectTimeout(60, TimeUnit.SECONDS) 
       .readTimeout(60, TimeUnit.SECONDS) 
       .writeTimeout(60, TimeUnit.SECONDS) 
       .build(); 

     retrofit = new Retrofit.Builder() 
       .baseUrl(WsPrefix) 
       .client(okHttpClient) 
       .build(); 
     return retrofit; 
    } 

通用接口

interface ApiInterface { 

     @GET 
     Call<ResponseBody> makeGetRequest(@Url String url, @HeaderMap() Map<String, String> header); 

     @POST 
     Call<ResponseBody> makePostRequest(@Url String url, @Body RequestBody requestBody, @HeaderMap() Map<String, String> header); 
} 

ApiCallback

public interface ApiCallback { 
     void success(String responseData); 
     void failure(String responseData); 
} 
1

這是一個有點棘手,但你需要使用自定義改造轉換器廠與自定義GsonBuilder它使用傳統JsonDeserializer。

此外,您應該定義一個使用CustomJsonDeserializer的接口(在我的示例中爲CustomResonse)。這不是必需的,但否則解串器將被用於請求。

public class CustomConverterFactory { 

    public static GsonConverterFactory create() { 
     return GsonConverterFactory.create(createGsonCustomDeserializer()); 
    } 

    public static Gson createGsonCustomJsonDeserializer() { 
     return new GsonBuilder() 
      .registerTypeAdapter(CustomResponse.class, new CustomJsonDeserializer()) 
      .serializeNulls() 
      .create(); 
    } 
} 

而對於解串器:

public class CustomJsonDeserializer implements JsonDeserializer<CustomResponse> { 

@Override 
public CustomResponse deserialize(final JsonElement json, final Type typeOfT, 
     final JsonDeserializationContext context) throws JsonParseException { 
    if (json.isJsonObject()) { 
     JsonObject jsonObject = json.getAsJsonObject(); 

     // Here you have to distinguish your class somehow 
     // Maybe read some Json field from your response 
     if (jsonObject.has("name")) { 
      JsonElement classes = jsonObject.get("name"); 
      ... 
      return context.deserialize(json, MyName.class); 
     } 
     ... 
     // Default fallback: Deserialize as a fallback object 
     return context.deserialize(json, MyFallback.class); 
    } else { 
     throw new IllegalStateException(); 
    } 
} 
+0

我知道這種方式..但它的一個很長的路要做到這一點..仍然感謝您的回覆.. –

+1

要麼你知道你想要的類,然後再執行請求,或者你必須這樣做。 – dipdipdip

+0

謝謝你,我仍然在尋找一些很好的解決方案..我有這種方式..爲計劃b。 –