2016-07-28 47 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。 –