Clover Coverage Report - Main Coverage Report
Coverage timestamp: ven dic 19 2014 16:47:52 EST
../../../../img/srcFileCovDistChart4.png 74% of files have more coverage
127   246   41   18,14
46   202   0,32   7
7     5,86  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  HttpUtils       Line # 29 127 41 39,4% 0.39444444
 
No Tests
 
1    package com.dreamcatcher.bicycle.util;
2   
3    import java.io.BufferedReader;
4    import java.io.IOException;
5    import java.io.InputStream;
6    import java.io.InputStreamReader;
7    import java.io.UnsupportedEncodingException;
8    import java.util.ArrayList;
9   
10    import org.apache.http.HttpResponse;
11    import org.apache.http.NameValuePair;
12    import org.apache.http.client.HttpClient;
13    import org.apache.http.client.entity.UrlEncodedFormEntity;
14    import org.apache.http.client.methods.HttpGet;
15    import org.apache.http.client.methods.HttpPost;
16    import org.apache.http.impl.client.DefaultHttpClient;
17    import org.apache.http.message.BasicNameValuePair;
18    import org.apache.http.params.BasicHttpParams;
19    import org.apache.http.params.HttpConnectionParams;
20    import org.apache.http.params.HttpParams;
21    import org.json.JSONArray;
22    import org.json.JSONException;
23    import org.json.JSONObject;
24   
25    import com.dreamcatcher.bicycle.exception.NetworkException;
26    import com.dreamcatcher.bicycle.vo.BicycleNumberInfo;
27    import com.dreamcatcher.bicycle.vo.CitySetting;
28   
 
29    public class HttpUtils {
30    private static final int REQUEST_TIME_OUT = 10 * 1000;
31    private static final int SO_TIME_OUT = 10 * 1000;
32   
33    /**
34    * update bicycles info from server and save it to local
35    */
 
36  5 toggle public static boolean getAllBicyclesInfoFromServer() throws IOException, NetworkException{
37  5 if(Utils.getNetworkInfo() == Constants.NetworkInfo.DISCONNECT){
38  0 throw new NetworkException();
39    }
40   
41  5 boolean success = false;
42  5 HttpClient httpClient = getHttpClient();
43   
44  5 CitySetting citySetting = GlobalSetting.getInstance().getCitySetting();
45  5 if(citySetting == null){
46  0 return false;
47    }
48  5 HttpGet httpGet = new HttpGet(citySetting.getAllBicyclesUrl());
49   
50  5 HttpParams params = new BasicHttpParams();
51  5 HttpConnectionParams.setConnectionTimeout(params, 5000);
52  5 httpGet.setParams(params);
53   
54  5 String jsonStr = null;
55  5 try {
56  5 HttpResponse response = httpClient.execute(httpGet);
57  5 jsonStr = getJsonDataFromInputStream(response.getEntity().getContent());
58   
59  5 if(jsonStr != null && !jsonStr.trim().equals("")){
60  5 int firstBrace = jsonStr.indexOf("{");
61  5 if(firstBrace < 0){
62  4 throw new IOException();
63    }
64  1 jsonStr = jsonStr.substring(firstBrace);
65   
66  1 Utils.setToDataset(jsonStr);
67  1 Utils.storeStringDataToLocal(Constants.LocalStoreTag.ALL_BICYCLE, jsonStr);
68  1 success = true;
69    }
70   
71    } catch (IOException e) {
72  4 e.printStackTrace();
73  4 throw e;
74    }
75  1 return success;
76    }
77   
78    /**
79    * get single bicycle station info
80    * @param id
81    * @return
82    */
 
83  0 toggle public static BicycleNumberInfo getSingleBicycleInfoFromHttp(int id) throws IOException, JSONException, NetworkException{
84  0 if(Utils.getNetworkInfo() == Constants.NetworkInfo.DISCONNECT){
85  0 throw new NetworkException();
86    }
87  0 HttpClient httpClient = getHttpClient();
88  0 CitySetting citySetting = GlobalSetting.getInstance().getCitySetting();
89   
90  0 HttpGet httpGet = new HttpGet(citySetting.getBicycleDetailUrl() + String.valueOf(id));
91  0 String jsonStr = null;
92  0 BicycleNumberInfo bicycleNumberInfo = null;
93  0 try {
94  0 HttpResponse response = httpClient.execute(httpGet);
95  0 jsonStr = getJsonDataFromInputStream(response.getEntity().getContent());
96   
97  0 if(jsonStr != null && !jsonStr.equals("")){
98  0 int firstBrace = jsonStr.trim().indexOf("{");
99  0 if(firstBrace < 0){
100  0 throw new IOException();
101    }
102   
103  0 jsonStr = jsonStr.substring(firstBrace);
104   
105  0 JSONObject jsonObject = new JSONObject(jsonStr);
106  0 JSONArray jsonArray = jsonObject.getJSONArray(Constants.BicycleJsonTag.STATION);
107   
108  0 for(int i = 0, total = jsonArray.length(); i < total; i++){
109  0 JSONObject jsonItem = jsonArray.getJSONObject(i);
110  0 int capacity = jsonItem.getInt(Constants.BicycleJsonTag.CAPACITY);
111  0 int available = jsonItem.getInt(Constants.BicycleJsonTag.AVAIABLE);
112   
113  0 bicycleNumberInfo = new BicycleNumberInfo(id, capacity, available);
114    }
115    }
116   
117    } catch (IOException e) {
118  0 e.printStackTrace();
119  0 throw e;
120    } catch (JSONException e) {
121  0 e.printStackTrace();
122  0 throw e;
123    }
124   
125  0 return bicycleNumberInfo;
126    }
127   
 
128  5 toggle private static String getJsonDataFromInputStream(InputStream inputStream){
129  5 String jsonStr = null;
130  5 try {
131  5 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
132  5 StringBuilder stringBuilder = new StringBuilder();
133  5 String line = null;
134  ? while((line= reader.readLine()) != null){
135  108 stringBuilder.append(line);
136    }
137  5 jsonStr = stringBuilder.toString();
138   
139  5 boolean needDecode = GlobalSetting.getInstance().getCitySetting().isNeedDecode();
140  5 if(needDecode){
141  0 jsonStr = Utils.decodeSZCode(jsonStr);
142    }
143    } catch (UnsupportedEncodingException e) {
144  0 e.printStackTrace();
145    } catch (IOException e) {
146  0 e.printStackTrace();
147    }
148  5 return jsonStr;
149    }
150   
 
151  1 toggle public static boolean checkVersion(String currentVersionName, int currentVersionCode) throws NetworkException, IOException, JSONException{
152  1 if(Utils.getNetworkInfo() == Constants.NetworkInfo.DISCONNECT){
153  0 throw new NetworkException();
154    }
155  1 boolean needUpdate = false;
156  1 HttpClient httpClient = getHttpClient();
157  1 HttpGet httpGet = new HttpGet(Constants.HttpUrl.VERSION_INFO_URL);
158   
159  1 try {
160  1 HttpResponse response = httpClient.execute(httpGet);
161  0 String jsonStr = getJsonDataFromInputStream(response.getEntity().getContent());
162  0 if(jsonStr != null && !jsonStr.equals("")){
163  0 JSONObject jsonObject = new JSONObject(jsonStr);
164  0 String versionName = jsonObject.getString("versionName");
165  0 int versionCode = jsonObject.getInt("versionCode");
166   
167  0 int versionNameCompareResult = compareStr(versionName, currentVersionName);
168  0 if(versionNameCompareResult > 0){
169  0 needUpdate = true;
170  0 }else if(versionNameCompareResult == 0){
171  0 if(versionCode > currentVersionCode){
172  0 needUpdate = true;
173    }
174    }
175    }
176    } catch (IOException e) {
177  1 e.printStackTrace();
178  1 throw e;
179    } catch (JSONException e) {
180  0 e.printStackTrace();
181  0 throw e;
182    }
183   
184  0 return needUpdate;
185    }
186   
 
187  0 toggle private static int compareStr(String first, String second){
188  0 int result = 0;
189  0 byte[] firstBytes = first.getBytes();
190  0 byte[] secondeBytes = second.getBytes();
191  0 int count = Math.min(firstBytes.length, secondeBytes.length);
192  0 int index = 0;
193  0 for(; index < count; index++){
194  0 if(firstBytes[index] > secondeBytes[index]){
195  0 result = 1;
196  0 break;
197  0 }else if(firstBytes[index] < secondeBytes[index]){
198  0 result = -1;
199  0 break;
200    }
201    }
202  0 if(index == count){
203  0 if(firstBytes.length > secondeBytes.length){
204  0 result = 1;
205  0 }else if(firstBytes.length == secondeBytes.length){
206  0 result = 0;
207    }else {
208  0 result = -1;
209    }
210    }
211  0 return result;
212    }
213   
 
214  7 toggle private static HttpClient getHttpClient(){
215  7 HttpParams params = new BasicHttpParams();
216  7 params.setParameter("charset", Constants.HttpSetting.HTTP_CONT_ENCODE);
217  7 HttpConnectionParams.setConnectionTimeout(params, REQUEST_TIME_OUT);
218  7 HttpConnectionParams.setSoTimeout(params, SO_TIME_OUT);
219  7 HttpClient httpClient = new DefaultHttpClient(params);
220   
221  7 return httpClient;
222    }
223   
 
224  1 toggle public static void sendFeedback(String msg) throws NetworkException, IOException{
225  1 if(Utils.getNetworkInfo() == Constants.NetworkInfo.DISCONNECT){
226  0 throw new NetworkException();
227    }
228  1 HttpClient httpClient = getHttpClient();
229  1 HttpPost httpPost = new HttpPost(Constants.HttpUrl.FEEDBACK_URL);
230   
231  1 ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
232  1 params.add(new BasicNameValuePair("msg", msg));
233   
234  1 httpPost.addHeader("charset", Constants.HttpSetting.HTTP_CONT_ENCODE);
235  1 httpPost.setEntity(new UrlEncodedFormEntity(params, Constants.HttpSetting.HTTP_CONT_ENCODE));
236  1 try {
237  1 HttpResponse response = httpClient.execute(httpPost);
238  0 int code = response.getStatusLine().getStatusCode();
239  0 if(code == 200){
240   
241    }
242    } catch (Exception e) {
243    // TODO: handle exception
244    }
245    }
246    }