긴 Url을 bit.ly처럼 짧은 주소로 바꿔야 될 경우가 있습니다;

제가 실제 상업용으로 개발할때 작성한 코드입니다 
가입하고 키를 받으시고 아래 " " 안에 넣어주시면되요


To get started you'll need a free bit.ly user account and API key - sign up at:

http://bit.ly/a/sign_up

Quickly access your private API key once you are signed in at:

http://bit.ly/a/your_api_key


참고&메뉴얼 : http://code.google.com/p/bitly-api/

public static String getShortUrl(String longurl) {
 String shortedUrl = "";
 BufferedReader in = null;
 try {
  List<NameValuePair> params = new ArrayList<NameValuePair>();
  params.add(new BasicNameValuePair("login",""));
  params.add(new BasicNameValuePair("apikey",""));
  params.add(new BasicNameValuePair("longUrl","http://"));
  params.add(new BasicNameValuePair("format","txt"));
  HttpHost host = new HttpHost("api.bit.ly/v3/shorten", -1, HttpHost.DEFAULT_SCHEME_NAME);
  URI u = URIUtils.createURI("http","api.bit.ly/v3/shorten", -1,"/v3/shorten" , 
    URLEncodedUtils.format(params,"UTF-8"),null);
  HttpGet httpget = new HttpGet(u);
  //Log.d("Lotto","URL : " + u.toString());
  HttpResponse response  = httpclient.execute(host,httpget);
  if(response.getStatusLine().getStatusCode() != 200) {
  } else {
   in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
   StringBuffer sb = new StringBuffer("");
   String line = "";
   String NL = System.getProperty("line.separator");
   while((line=in.readLine()) != null) {
    sb.append(line + NL);
   }
   in.close();
   shortedUrl = sb.toString();
  }
 }catch(Exception e) {
  e.printStackTrace();
 }finally  {
  if(in != null) {
   try {
    in.close();
   }catch(IOException e) {
    e.printStackTrace();
   }
  }
 }
 return shortedUrl;
}


 

profile

안녕하세요 반갑습니다.