要为我的改造端点返回模拟JSON数据,我创建了一个自定义拦截器并将其添加到OkHttpClient进行Retrofit,如下所示:client.addInterceptor(new HttpInterceptor());
我创建的实现工作正常,但它的可扩展性不高。所有端点及其模拟数据都在一个文件中处理,如下所示:
public class HttpInterceptor implements Interceptor {
private static final int TEST_QUANTITY = 10;
@Override
public Response intercept(Chain chain) throws IOException {
Response response;
String responseString;
String uriPath = chain.request().url().uri().getPath();
if (uriPath.equalsIgnoreCase("/geofences")) {
responseString = buildMockGeofences(TEST_QUANTITY);
} else if (uriPath.equalsIgnoreCase("/retailers")) {
responseString = buildMockRetailers(TEST_QUANTITY);
} else if (uriPath.equalsIgnoreCase("/products")) {
responseString = buildMockProducts(TEST_QUANTITY);
} else if (uriPath.matches("/retailers/(\\d+)")) {
String stringId = uriPath.replaceAll("[^0-9]", "");
int id = Integer.parseInt(stringId);
responseString = getRetailer(id);
} else if (uriPath.matches("\\/products\\/\\w+")) {
String id = uriPath.substring(uriPath.lastIndexOf('/') + 1);
responseString = getProduct(id);
} else if (uriPath.matches("/geofences/(\\d+)")) {
String stringId = uriPath.replaceAll("[^0-9]", "");
int id = Integer.parseInt(stringId);
responseString = getGeofence(id);
} else {
responseString = "{}";
}
response = new Response.Builder()
.code(200)
.message(responseString)
.request(chain.request())
.protocol(Protocol.HTTP_1_0)
.body(ResponseBody.create(MediaType.parse("application/json"), responseString.getBytes()))
.addHeader("content-type", "application/json")
.build();
return response;
}
private String buildMockProducts(int testQuantity) {
String jsonProducts = "";
for (int i = 1; i <= testQuantity; i++) {
jsonProducts += "{\"_id\" : \" " + i + " \" , " +
"\"name\" : \"Product" + i + "\" , " +
"\"category\" : \"category\" , " +
"\"requiredStamps\" : " + i + " , " +
"\"size\" : \"n.a.\" , " +
"\"price\" : \"€15,-\" , " +
"\"description\" : \"description\" , " +
"\"stickerId\" : \"" + stickerIds().get(i) + "\" }";
if (i != testQuantity)
jsonProducts += ",";
}
return "[" + jsonProducts + "]";
}
private List<String> stickerIds() {
List<String> stickerIdList = new ArrayList<>();
stickerIdList.add(null);
stickerIdList.add("0798e8199c4fbbaa");
stickerIdList.add("496fa572b8a2e03d");
stickerIdList.add("4a1d5cdd6b0764f5");
stickerIdList.add("3deac01afb311b13");
stickerIdList.add("9ee2a14a92025ace");
stickerIdList.add("494e443257a4dd95");
stickerIdList.add("fa3dd8e30c872245");
stickerIdList.add("5c81ccfc21dedb74");
stickerIdList.add("0b642e5658453ce1");
stickerIdList.add("9d0c9aa50f971cd7");
return stickerIdList;
}
private String getProduct(String id) {
return "{\"_id\" : \" " + id + " \" , " +
"\"name\" : \"Product\" , " +
"\"category\" : \"category\" , " +
"\"requiredStamps\" : " + 25 + " , " +
"\"size\" : \"n.a.\" , " +
"\"price\" : \"€15,-\" , " +
"\"description\" : \"description\" , " +
"\"stickerId\" : \"" + id + "\" }";
}
private String buildMockRetailers(int numberOfRetailers) {
String jsonRetailers = "";
for (int i = 1; i <= numberOfRetailers; i++) {
jsonRetailers += "{\"_id\" : \" " + i + " \" , " +
"\"name\" : \"Retailer Name " + i + "\" , " +
"\"street\" : \"straatnaam\" , " +
"\"housenumber\" : \" " + i + " \" , " +
"\"postalCode\" : \"1023BH\" , " +
"\"city\" : \"City\" , " +
"\"geofence\" : \"" + i + "\" }";
if (i != numberOfRetailers)
jsonRetailers += ","; // don't add closing comma
}
return "[" + jsonRetailers + "]";
}
private String getRetailer(int id) {
return "[{\"_id\" : \" " + id + " \" , " +
"\"name\" : \"Retailer Name " + id + "\" , " +
"\"street\" : \"straatnaam\" , " +
"\"housenumber\" : \" " + id + " \" , " +
"\"postalCode\" : \"1023BH\" , " +
"\"city\" : \"City\" , " +
"\"geofence\" : \"" + id + "\" }]";
}
private String getGeofence(int id) {
return "[{\"_id\" : \" " + id + " \" , " +
"\"title\" : \"TEST_GEOFENCE" + id + "\" , " +
"\"latitude\" : " + genRandLatitude() + " , " +
"\"longitude\" : " + genRandLongitude() + " , " +
"\"radius\" : 500 , " +
"\"retailer\" : \"" + id + "\" }]";
}
private String buildMockGeofences(int numberOfGeofences) {
String jsonGeofences = "";
for (int i = 1; i <= numberOfGeofences; i++) {
jsonGeofences += "{\"_id\" : \"58ca9bc5384ec0172c94423" + i + "\" , " +
"\"title\" : \"TEST_GEOFENCE" + i + "\" , " +
"\"latitude\" : " + genRandLatitude() + " , " +
"\"longitude\" : " + genRandLongitude() + " , " +
"\"radius\" : 500 , " +
"\"retailer\" : \"" + i + "\" }";
if (i != numberOfGeofences)
jsonGeofences += ",";
}
return "[" + jsonGeofences + "]";
}
private double genRandLatitude() {
Random r = new Random();
double randomValue = 52.322632 + (52.397274 - 52.322632) * r.nextDouble();
return Math.floor(randomValue * 1000000) / 1000000;
}
private double genRandLongitude() {
Random r = new Random();
double randomValue = 4.845314 + (4.952431 - 4.845314) * r.nextDouble();
return Math.floor(randomValue * 1000000) / 1000000;
}
如何在Retrofit的httpclient中添加多个拦截器?因为如果我只是分开这样的逻辑:
client.addInterceptor(new GeofenceHttpInterceptor());
client.addInterceptor(new RetailerHttpInterceptor());
client.addInterceptor(new ProductHttpInterceptor());
它仍将拦截第一个拦截器中的url。任何帮助都很高兴。