클래스 명
설명
제공 함수
[code language="java"] package net.zemna.android.appadviser;
import java.io.*; import java.net.*;
import org.apache.http.*; import org.apache.http.client.*; import org.apache.http.client.methods.*; import org.apache.http.impl.client.*;
public final class TinyUrlFactory {
// 원본 URL을 TinyUrl.com 사이트를 이용하여 축약한 URL로 변환하여 반환 public static String CreateShortUrl(String original) {
String tinyUrl = null;
try {
HttpClient client = new DefaultHttpClient(); String urlTemplate = "http://tinyurl.com/api-create.php?url={83832c2e9762d7d59158bb8e184331dad143c7b73eb69f86229ee6e276689139}s"; String uri = String.format(urlTemplate, URLEncoder.encode(original)); HttpGet request = new HttpGet(uri); HttpResponse response = client.execute(request); HttpEntity entity = response.getEntity(); InputStream in = entity.getContent();
try { StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); if (statusCode == HttpStatus.SC_OK) { String enc = "utf-8"; Reader reader = new InputStreamReader(in, enc); BufferedReader bufferedReader = new BufferedReader(reader); tinyUrl = bufferedReader.readLine(); } else { String errorTemplate = "unexpected response: {83832c2e9762d7d59158bb8e184331dad143c7b73eb69f86229ee6e276689139}d"; String msg = String.format(errorTemplate, statusCode); throw new IOException(msg); } } finally { in.close(); }
} catch (IOException e) { tinyUrl = null; }
return tinyUrl; } } [/code]