EMMA Coverage Report (generated Tue Jun 26 14:54:12 CEST 2012)
[all classes][org.tomdroid.sync.web]

COVERAGE SUMMARY FOR SOURCE FILE [WebConnection.java]

nameclass, %method, %block, %line, %
WebConnection.java0%   (0/1)0%   (0/4)0%   (0/122)0%   (0/44)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class WebConnection0%   (0/1)0%   (0/4)0%   (0/122)0%   (0/44)
WebConnection (): void 0%   (0/1)0%   (0/3)0%   (0/1)
convertStreamToString (InputStream): String 0%   (0/1)0%   (0/60)0%   (0/14)
execute (HttpUriRequest): HttpResponse 0%   (0/1)0%   (0/30)0%   (0/15)
parseResponse (HttpResponse): String 0%   (0/1)0%   (0/29)0%   (0/14)

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 */
23package org.tomdroid.sync.web;
24 
25import java.io.BufferedReader;
26import java.io.IOException;
27import java.io.InputStream;
28import java.io.InputStreamReader;
29import java.net.UnknownHostException;
30 
31import org.apache.http.HttpEntity;
32import org.apache.http.HttpResponse;
33import org.apache.http.client.ClientProtocolException;
34import org.apache.http.client.methods.HttpUriRequest;
35import org.apache.http.impl.client.DefaultHttpClient;
36import org.tomdroid.ui.Tomdroid;
37 
38import android.util.Log;
39 
40public 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}

[all classes][org.tomdroid.sync.web]
EMMA 0.0.0 (unsupported private build) (C) Vladimir Roubtsov