@@ -23,12 +23,12 @@ import java.net.URL
23
23
* 2018 April 17 - Tuesday - 12:56 PM
24
24
**/
25
25
26
- class ApiServices (context : Context ) {
27
-
26
+ class ApiServices (context : Context )
27
+ {
28
28
var mResponseCode = 0
29
29
private val apiPath: String
30
- val deviceID: String
31
- private val sharedPreferenceData: SharedPreferenceData
30
+ private val deviceID: String
31
+ private val sharedPreferenceData: SharedPreferenceData = SharedPreferenceData (context)
32
32
33
33
/* *
34
34
* get device name method
@@ -37,20 +37,23 @@ class ApiServices(context: Context) {
37
37
* this method will get the name of the device
38
38
*/
39
39
val deviceName: String
40
- get() {
40
+ get()
41
+ {
41
42
val manufacturer = Build .MANUFACTURER
42
43
val model = Build .MODEL
43
44
44
- return if (model.startsWith(manufacturer)) {
45
+ return if (model.startsWith(manufacturer))
46
+ {
45
47
capitalizeString(model)
46
- } else {
48
+ }
49
+ else
50
+ {
47
51
capitalizeString(" $manufacturer $model " )
48
52
}
49
53
}
50
54
51
- init {
52
- this .sharedPreferenceData = SharedPreferenceData (context)
53
-
55
+ init
56
+ {
54
57
// set api path before using it
55
58
// this path will consist of the url which is repeated in every api
56
59
// Ex: http://www.example.com/api/
@@ -68,16 +71,21 @@ class ApiServices(context: Context) {
68
71
* @param string - string to capitalize
69
72
* return - will return the string which was passed in capitalize form
70
73
*/
71
- private fun capitalizeString (string : String? ): String {
72
- if (string == null || string.isEmpty()) {
74
+ private fun capitalizeString (string : String? ): String
75
+ {
76
+ if (string == null || string.isEmpty())
77
+ {
73
78
return " "
74
79
}
75
80
76
81
val first = string[0 ]
77
82
78
- return if (Character .isUpperCase(first)) {
83
+ return if (Character .isUpperCase(first))
84
+ {
79
85
string
80
- } else {
86
+ }
87
+ else
88
+ {
81
89
Character .toUpperCase(first) + string.substring(1 )
82
90
}
83
91
}
@@ -127,11 +135,13 @@ class ApiServices(context: Context) {
127
135
*
128
136
*
129
137
* ***********************************************************************************************
130
- */
138
+ * */
131
139
fun makeAPICall (apiName : String , requestMethod : String ,
132
140
parameters : Boolean , values : JSONObject ,
133
- hasToken : Boolean ): String? {
134
- try {
141
+ hasToken : Boolean ): String?
142
+ {
143
+ try
144
+ {
135
145
val result: String
136
146
val url = URL (apiPath + apiName)
137
147
@@ -141,7 +151,8 @@ class ApiServices(context: Context) {
141
151
httpURLConnection.addRequestProperty(" Content-Type" , " application/json; charset=utf-8" )
142
152
httpURLConnection.addRequestProperty(" IsDeviceMode" , " 1" )
143
153
144
- if (hasToken) {
154
+ if (hasToken)
155
+ {
145
156
httpURLConnection.addRequestProperty(" x-access-token" , sharedPreferenceData.getValue(" token" ))
146
157
Log .e(TAG , " makeAPICall: x-access-token is: " + sharedPreferenceData.getValue(" token" ))
147
158
}
@@ -150,7 +161,8 @@ class ApiServices(context: Context) {
150
161
httpURLConnection.setRequestProperty(" Connection" , " close" )
151
162
Log .e(TAG , " makeAPICall: api = $url values = $values " )
152
163
153
- if (parameters) {
164
+ if (parameters)
165
+ {
154
166
httpURLConnection.doOutput = true
155
167
val outputStream = DataOutputStream (httpURLConnection.outputStream)
156
168
outputStream.writeBytes(values.toString())
@@ -162,27 +174,32 @@ class ApiServices(context: Context) {
162
174
mResponseCode = responseCode
163
175
Log .e(TAG , " makeAPICall: response code from api is: $mResponseCode " )
164
176
165
- if (responseCode == 200 ) {
177
+ if (responseCode == 200 )
178
+ {
166
179
val bufferedReader = BufferedReader (InputStreamReader (httpURLConnection.inputStream))
167
180
val stringBuilder = StringBuilder ()
168
181
var line: String? = null
169
182
170
- while ({line = bufferedReader.readLine(); line}() != null ) {
183
+ while ({line = bufferedReader.readLine(); line}() != null )
184
+ {
171
185
stringBuilder.append(line).append(" \n " )
172
186
}
173
187
174
188
bufferedReader.close()
175
189
result = stringBuilder.toString()
176
190
Log .e(TAG , " makeAPICall: result from server is: $result " )
177
- } else {
191
+ }
192
+ else
193
+ {
178
194
val responseMessage = httpURLConnection.responseMessage
179
195
Log .e(TAG , " makeAPICall: response message: $responseMessage " )
180
196
181
197
val bufferedReader = BufferedReader (InputStreamReader (httpURLConnection.errorStream))
182
198
val stringBuilder = StringBuilder ()
183
199
var line: String? = null
184
200
185
- while ({line = bufferedReader.readLine(); line}() != null ) {
201
+ while ({line = bufferedReader.readLine(); line}() != null )
202
+ {
186
203
stringBuilder.append(line).append(" \n " )
187
204
}
188
205
@@ -192,12 +209,13 @@ class ApiServices(context: Context) {
192
209
}
193
210
194
211
return result
195
- } catch (e: Exception ) {
212
+ }
213
+ catch (e: Exception )
214
+ {
196
215
Log .e(TAG , " makeAPICall: in web api services class:\n " )
197
216
e.printStackTrace()
198
217
return null
199
218
}
200
-
201
219
}
202
220
203
221
/* *
@@ -218,12 +236,14 @@ class ApiServices(context: Context) {
218
236
* where integer will be the response code
219
237
* and bitmap will be the file uploaded.
220
238
*/
221
- fun getBitmapData (apiPath : String , requestType : String , parameter : String? ): Pair <Int , Bitmap > {
239
+ fun getBitmapData (apiPath : String , requestType : String , parameter : String? ): Pair <Int , Bitmap >
240
+ {
222
241
var resultVal: Bitmap ? = null
223
242
val iStream: InputStream
224
243
var responseCode = 0
225
244
226
- try {
245
+ try
246
+ {
227
247
val urlConnect = URL (apiPath)
228
248
val urlConnection = urlConnect.openConnection() as HttpURLConnection
229
249
@@ -236,36 +256,47 @@ class ApiServices(context: Context) {
236
256
Log .e(TAG , " getBitmapData: x-access-token is: " + sharedPreferenceData.getValue(" token" ))
237
257
urlConnection.connectTimeout = 30000
238
258
239
- if (parameter != null ) {
259
+ if (parameter != null )
260
+ {
240
261
urlConnection.doOutput = true
241
262
val outputStream = DataOutputStream (urlConnection.outputStream)
242
263
outputStream.writeBytes(parameter)
243
264
outputStream.flush()
244
265
outputStream.close()
245
266
}
246
267
247
- try {
268
+ try
269
+ {
248
270
responseCode = urlConnection.responseCode
249
271
Log .e(TAG , " Response Code from api is -- $responseCode " )
250
272
251
- if (responseCode == 200 ) {
273
+ if (responseCode == 200 )
274
+ {
252
275
iStream = urlConnection.inputStream
253
276
254
277
/* Creating a bitmap from the stream returned from the apiPath */
255
278
resultVal = BitmapFactory .decodeStream(iStream)
256
279
Log .e(TAG , " getBitmapData: result from server is: " + resultVal!! )
257
- } else {
280
+ }
281
+ else
282
+ {
258
283
val str = urlConnection.responseMessage
259
284
Log .e(TAG , " getBitmapData: response message from api is:$str " )
260
285
}
261
- } catch (ex: Exception ) {
286
+ }
287
+ catch (ex: Exception )
288
+ {
262
289
Log .e(TAG , " getBitmapData: exception while downloading images from api:\n " )
263
290
ex.printStackTrace()
264
- } finally {
291
+ }
292
+ finally
293
+ {
265
294
urlConnection.disconnect()
266
295
Log .e(TAG , " URL in finally is >$apiPath <-->$resultVal " )
267
296
}
268
- } catch (e: Exception ) {
297
+ }
298
+ catch (e: Exception )
299
+ {
269
300
Log .e(TAG , " Exception while geting bitmap:" )
270
301
e.printStackTrace()
271
302
return Pair (responseCode, resultVal)
@@ -274,7 +305,8 @@ class ApiServices(context: Context) {
274
305
return Pair (responseCode, resultVal)
275
306
}
276
307
277
- companion object {
308
+ companion object
309
+ {
278
310
private val TAG = ApiServices ::class .java.simpleName
279
311
}
280
312
}
0 commit comments