Java tutorial
//package com.java2s; /* * Copyright (C) 2014 The AppCan Open Source Project. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ import android.net.Uri; public class Main { public static String makeUrl(String inBaseUrl, String inUrl) { if (null == inUrl || inUrl.length() == 0) { return null; } if (null == inBaseUrl || inBaseUrl.length() == 0) { return null; } // inUrl = URLDecoder.decode(inUrl); if (uriHasSchema(inUrl) || inUrl.startsWith("/")) { return inUrl; } // String oldBaseUrl = inBaseUrl; // ../../ int index = inUrl.indexOf("../"); int layer = 0; while (index != -1) { layer++; inUrl = inUrl.substring(index + 3, inUrl.length()); index = inUrl.indexOf("../"); } // Uri path = Uri.parse(inBaseUrl); // String port = path.getPort() != -1 ? // ":"+String.valueOf(path.getPort()) : ""; // inBaseUrl = path.getScheme() // +"://"+path.getHost()+port+path.getPath(); int count = inBaseUrl.lastIndexOf(47); while (layer >= 0) { inBaseUrl = inBaseUrl.substring(0, count); count = inBaseUrl.lastIndexOf(47); layer--; if (count == -1) { break; } } inBaseUrl += "/" + inUrl; return inBaseUrl; } public static boolean uriHasSchema(String uri) { if (uri == null || uri.length() == 0) { return false; } String mUri = null; int i = uri.indexOf('?'); if (i > 0) { mUri = uri.substring(0, i); } else { mUri = uri; } final Uri path = Uri.parse(mUri); if (path != null && path.getScheme() != null) { return true; } else { return false; } } }