package com.novelbook.android.netapi; import android.util.Log; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class UrlFactory { public static String getHost(String uri) { String key = "master"; if (uri.startsWith("/api/search/")) { key = "search"; } else if (uri.startsWith("/api/n/")) { key = "novel"; } else if (uri.startsWith("/api/novels/")) { key = "novelsbydot"; } else if (uri.startsWith("/api/r/")) { key = "report"; } else if (uri.startsWith("/api/page/")) { key = "page"; } else if (uri.startsWith("/api/u/")) { key = "user"; } return key; } public static String getUrl(JSONObject host, String uri, int index) throws JSONException { if (uri == null) return ""; String key = getHost(uri); JSONArray us = null; if (host != null) { us = host.getJSONArray(key); if (us != null && us.length() > index) { return us.getString(index) ;//+ uri; } } // return ""; int value = (us == null || us.length()==0) ? index : index - us.length(); int second = 90 + 7; int a1 = value / 2 +1; int a2 = value % 2; String url = String.format("http://%s%s%s.%s%s%s%s.com", key.charAt(0), (char)(second + a2), a1, "xiao", "shuo", "fen", "xiang"); Log.d("randomurl", "getUrl: " + url); return url; } public static void main(String[] args) { /*String[] keys = new String[] {"/api/search/", "/api/n/", "/api/novels/", "/api/r/", "/api/page/", "/api/u/", "/api/g/"}; String text = FileUtil.read("C:\\MyWorkSpace\\WorkRoom\\cms4\\app\\xiaoshuofenxiang\\web\\api\\g\\Setting.json", "utf-8"); JSONObject hosts = JSONObject.parseObject(text).getJSONObject("data").getJSONObject("hosts"); for (String key : keys ) { RandomHost rh = new RandomHost(hosts, key); for (int i = 0; i < 11; i++) { String url = rh.next(); System.out.println(String.format("for %s and index = %s, url=%s", key, i, url)); } }*/ } }