Initial commit
[mustard2:mustard2.git] / src / org / mustard2 / urlshortener / B1tit.java
1 package org.mustard2.urlshortener;
2
3 import java.net.MalformedURLException;
4 import java.net.URL;
5 import java.util.ArrayList;
6 import java.util.HashMap;
7
8 import org.apache.http.NameValuePair;
9 import org.apache.http.message.BasicNameValuePair;
10 import org.json.JSONObject;
11 import org.mustard2.android.MustardException;
12 import org.mustard2.util.HttpManager;
13
14 import android.content.Context;
15
16 public class B1tit implements UrlShortener {
17
18         protected Context mContext;
19         protected String b1tUrl = "http://b1t.it/";
20         
21         public B1tit(Context context) {
22                 mContext=context;
23         }
24
25         @Override
26         public String doShort(String longUrl, HashMap<String, String> params)
27                         throws MustardException {
28                 return doShort(longUrl);
29         }
30
31         @Override
32         public String doShort(String longUrl) throws MustardException {
33                 URL uri = null;
34                 try {
35                         uri =new URL(b1tUrl);
36                 } catch (MalformedURLException e) {
37                         throw new MustardException(e.getMessage());
38                 }
39                 HttpManager hm = new HttpManager(mContext,uri.getHost());
40                 ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
41                 params.add(new BasicNameValuePair("url", longUrl));
42                 try {
43                         JSONObject o = hm.getJsonObject(b1tUrl, HttpManager.POST,params);
44                         String b1t = o.getString("id");
45                         return b1tUrl + b1t;
46                 } catch (Exception e) {
47                         throw new MustardException(e.getMessage());
48                 }
49         }
50
51         @Override
52         public String getShorterName() {
53                 return "b1t.it";
54         }
55         
56
57 }