| 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.BufferedReader; |
| 26 | import java.io.IOException; |
| 27 | import java.io.InputStream; |
| 28 | import java.io.InputStreamReader; |
| 29 | import java.net.UnknownHostException; |
| 30 | |
| 31 | import org.apache.http.HttpEntity; |
| 32 | import org.apache.http.HttpResponse; |
| 33 | import org.apache.http.client.ClientProtocolException; |
| 34 | import org.apache.http.client.methods.HttpUriRequest; |
| 35 | import org.apache.http.impl.client.DefaultHttpClient; |
| 36 | import org.tomdroid.ui.Tomdroid; |
| 37 | |
| 38 | import android.util.Log; |
| 39 | |
| 40 | public abstract class WebConnection { |
| 41 | |
| 42 | private static final String TAG = "WebConnection"; |
| 43 | |
| 44 | public abstract String get(String uri) throws UnknownHostException; |
| 45 | public abstract String put(String uri, String data) throws UnknownHostException; |
| 46 | |
| 47 | private static String convertStreamToString(InputStream is) { |
| 48 | /* |
| 49 | * To convert the InputStream to String we use the BufferedReader.readLine() |
| 50 | * method. We iterate until the BufferedReader return null which means |
| 51 | * there's no more data to read. Each line will appended to a StringBuilder |
| 52 | * and returned as String. |
| 53 | */ |
| 54 | BufferedReader reader = new BufferedReader(new InputStreamReader(is)); |
| 55 | StringBuilder sb = new StringBuilder(); |
| 56 | |
| 57 | String line = null; |
| 58 | try { |
| 59 | while ((line = reader.readLine()) != null) { |
| 60 | sb.append(line + "\n"); |
| 61 | } |
| 62 | } catch (IOException e) { |
| 63 | e.printStackTrace(); |
| 64 | } finally { |
| 65 | try { |
| 66 | is.close(); |
| 67 | } catch (IOException e) { |
| 68 | e.printStackTrace(); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return sb.toString(); |
| 73 | } |
| 74 | |
| 75 | protected String parseResponse(HttpResponse response) { |
| 76 | |
| 77 | if (response == null) |
| 78 | return ""; |
| 79 | |
| 80 | String result = null; |
| 81 | |
| 82 | // Examine the response status |
| 83 | if (Tomdroid.LOGGING_ENABLED) Log.i(TAG, "Response status : "+response.getStatusLine().toString()); |
| 84 | |
| 85 | // Get hold of the response entity |
| 86 | HttpEntity entity = response.getEntity(); |
| 87 | // If the response does not enclose an entity, there is no need |
| 88 | // to worry about connection release |
| 89 | |
| 90 | if (entity != null) { |
| 91 | |
| 92 | try { |
| 93 | InputStream instream; |
| 94 | |
| 95 | instream = entity.getContent(); |
| 96 | |
| 97 | result = convertStreamToString(instream); |
| 98 | |
| 99 | if (Tomdroid.LOGGING_ENABLED) Log.i(TAG, "Received : "+result); |
| 100 | |
| 101 | // Closing the input stream will trigger connection release |
| 102 | instream.close(); |
| 103 | |
| 104 | } catch (IllegalStateException e) { |
| 105 | // TODO Auto-generated catch block |
| 106 | e.printStackTrace(); |
| 107 | } catch (IOException e) { |
| 108 | // TODO Auto-generated catch block |
| 109 | e.printStackTrace(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return result; |
| 114 | } |
| 115 | |
| 116 | protected HttpResponse execute(HttpUriRequest request) throws UnknownHostException { |
| 117 | |
| 118 | DefaultHttpClient httpclient = new DefaultHttpClient(); |
| 119 | |
| 120 | try { |
| 121 | // Execute the request |
| 122 | HttpResponse response = httpclient.execute(request); |
| 123 | return response; |
| 124 | |
| 125 | }catch (UnknownHostException e){ |
| 126 | throw e; |
| 127 | } catch (ClientProtocolException e) { |
| 128 | e.printStackTrace(); |
| 129 | } catch (IOException e) { |
| 130 | e.printStackTrace(); |
| 131 | } catch (IllegalArgumentException e) { |
| 132 | e.printStackTrace(); |
| 133 | } catch (IllegalStateException e) { |
| 134 | e.printStackTrace(); |
| 135 | } |
| 136 | |
| 137 | return null; |
| 138 | } |
| 139 | } |