Java URL Connection urlifyMap(Long nid, double latitude, double longitude)

Here you can find the source of urlifyMap(Long nid, double latitude, double longitude)

Description

urlify Map

License

Apache License

Declaration

private static String urlifyMap(Long nid, double latitude, double longitude) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class Main {
    private static String urlifyMap(Long nid, double latitude, double longitude) {

        String url = "http://mapas.sapo.pt/cmap/cmap.html?sz=640,960&ll=" + latitude + "," + longitude
                + "&z=16&t=m&mks=" + longitude + "," + latitude + ",0,asd," + "&nid=" + nid;

        return shortenUrl(url);
    }/*from ww w  .ja  v  a  2s . c o  m*/

    private static String shortenUrl(String url) {
        String shortUrl = "";

        try {
            URLConnection conn = new URL("https://www.googleapis.com/urlshortener/v1/url").openConnection();
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-Type", "application/json");
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write("{\"longUrl\":\"" + url + "\"}");
            wr.flush();

            // Get the response
            BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;

            while ((line = rd.readLine()) != null) {
                if (line.indexOf("id") > -1) {
                    //hack
                    shortUrl = line.substring(8, line.length() - 2);
                    break;
                }
            }

            wr.close();
            rd.close();
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        return shortUrl;

    }
}

Related

  1. slurpURLNoExceptions(URL u)
  2. url2list(String sURL, String encoding)
  3. urlContents(String urlString)
  4. urlExists(String urlString)
  5. URLExists(URL url, StringBuffer errorMsg)
  6. urlToHtm(String word, String findurl, String path)
  7. urlToReader(URL url)
  8. urlToString(String url)
  9. urlToText(@Nonnull String url)