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 |
|
|
|
|
| 39,4% |
Uncovered Elements: 109 (180) |
Complexity: 41 |
Complexity Density: 0,32 |
|
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 |
|
|
35 |
|
|
|
|
| 85,3% |
Uncovered Elements: 5 (34) |
Complexity: 7 |
Complexity Density: 0,27 |
|
36 |
5
|
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 |
|
|
80 |
|
@param |
81 |
|
@return |
82 |
|
|
|
|
| 0% |
Uncovered Elements: 35 (35) |
Complexity: 8 |
Complexity Density: 0,3 |
|
83 |
0
|
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 |
|
|
|
|
| 66,7% |
Uncovered Elements: 6 (18) |
Complexity: 5 |
Complexity Density: 0,36 |
|
128 |
5
|
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 |
|
|
|
|
| 27,3% |
Uncovered Elements: 24 (33) |
Complexity: 9 |
Complexity Density: 0,39 |
|
151 |
1
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 31 (31) |
Complexity: 7 |
Complexity Density: 0,37 |
|
187 |
0
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0,17 |
|
214 |
7
|
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 |
|
|
|
|
| 62,5% |
Uncovered Elements: 6 (16) |
Complexity: 4 |
Complexity Density: 0,33 |
|
224 |
1
|
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 |
|
|
244 |
|
} |
245 |
|
} |
246 |
|
} |