Java tutorial
//package com.java2s; /* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import android.net.Uri; import android.util.Log; import java.util.regex.Pattern; public class Main { /** * Jarvis URL. Use {@link String#format(String, Object...)} to insert * REST data into URI. */ public static String API_ROOT = ""; /** * Create a URL from an API call (or "action string") */ public static String getPageURL(String call) { Log.i("Jarvis", String.format("Call: %s", call)); // Call's parts String function = ""; String action = ""; String data = ""; // Final URL String url = ""; Boolean isInternal = true; // This is weird left-over support for external URLs // Check if this is not a Jarvis URL if (call.length() >= 4 && call.substring(0, 4).equals("http")) { Integer length = API_ROOT.length(); if (call.substring(0, length).equals(API_ROOT)) { call = call.substring(length + 1); call = call.replace("/", " "); } else { url = call; isInternal = false; } } if (isInternal) { // Encode URI data Pattern pattern = Pattern.compile(" "); String[] parts = pattern.split(call, 3); function = Uri.encode(parts[0]); if (parts.length > 1) { action = Uri.encode(parts[1]); } if (parts.length > 2) { data = Uri.encode(parts[2]); } // Final format url = String.format("%s/api/%s/%s/%s", API_ROOT, function, action, data); } Log.d("Jarvis", String.format("URL: %s", url)); return url; } }