1 | /* |
2 | * Tomdroid |
3 | * Tomboy on Android |
4 | * http://www.launchpad.net/tomdroid |
5 | * |
6 | * Copyright 2009, Benoit Garret <benoit.garret_launchpad@gadz.org> |
7 | * |
8 | * This file is part of Tomdroid. |
9 | * |
10 | * Tomdroid is free software: you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License as published by |
12 | * the Free Software Foundation, either version 3 of the License, or |
13 | * (at your option) any later version. |
14 | * |
15 | * Tomdroid is distributed in the hope that it will be useful, |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | * GNU General Public License for more details. |
19 | * |
20 | * You should have received a copy of the GNU General Public License |
21 | * along with Tomdroid. If not, see <http://www.gnu.org/licenses/>. |
22 | */ |
23 | package org.tomdroid.sync.web; |
24 | |
25 | import java.io.UnsupportedEncodingException; |
26 | import java.net.UnknownHostException; |
27 | |
28 | import org.apache.http.HttpResponse; |
29 | import org.apache.http.client.methods.HttpGet; |
30 | import org.apache.http.client.methods.HttpPut; |
31 | import org.apache.http.entity.StringEntity; |
32 | |
33 | public class AnonymousConnection extends WebConnection { |
34 | |
35 | @Override |
36 | public String get(String uri) throws UnknownHostException |
37 | { |
38 | // Prepare a request object |
39 | HttpGet httpGet = new HttpGet(uri); |
40 | HttpResponse response = execute(httpGet); |
41 | return parseResponse(response); |
42 | } |
43 | |
44 | @Override |
45 | public String put(String uri, String data) throws UnknownHostException { |
46 | |
47 | // Prepare a request object |
48 | HttpPut httpPut = new HttpPut(uri); |
49 | |
50 | try { |
51 | // The default http content charset is ISO-8859-1, JSON requires UTF-8 |
52 | httpPut.setEntity(new StringEntity(data, "UTF-8")); |
53 | } catch (UnsupportedEncodingException e1) { |
54 | e1.printStackTrace(); |
55 | return null; |
56 | } |
57 | |
58 | httpPut.setHeader("Content-Type", "application/json"); |
59 | HttpResponse response = execute(httpPut); |
60 | return parseResponse(response); |
61 | } |
62 | } |