Convert URL query string to Map - Android java.net

Android examples for java.net:URL

Description

Convert URL query string to Map

Demo Code

import android.graphics.Paint;
import android.graphics.Paint;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Pattern;

public class Main{

    public static Map<String, String> string2Map(String url) {
        Map<String, String> map = new HashMap<String, String>();
        int index = url.indexOf('?');
        String newURL = url.substring(index + 1);
        try {// w ww .  j av  a2s .c  o m
            String[] splitStrings = newURL.split("&");
            for (String string : splitStrings) {
                String[] keyValues = string.split("=");
                map.put(keyValues[0], keyValues[1]);
            }
        } catch (NullPointerException e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return map;
    }

}

Related Tutorials