Skip to content

Commit 347d2f8

Browse files
committed
1. Added query for checking if table exists in database.
1 parent cdc7761 commit 347d2f8

File tree

5 files changed

+259
-104
lines changed

5 files changed

+259
-104
lines changed

.idea/caches/build_file_checksums.ser

0 Bytes
Binary file not shown.

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/java/com/amit/kotlib/api/ApiServices.kt

Lines changed: 67 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ import java.net.URL
2323
* 2018 April 17 - Tuesday - 12:56 PM
2424
**/
2525

26-
class ApiServices(context: Context) {
27-
26+
class ApiServices(context: Context)
27+
{
2828
var mResponseCode = 0
2929
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)
3232

3333
/**
3434
* get device name method
@@ -37,20 +37,23 @@ class ApiServices(context: Context) {
3737
* this method will get the name of the device
3838
*/
3939
val deviceName: String
40-
get() {
40+
get()
41+
{
4142
val manufacturer = Build.MANUFACTURER
4243
val model = Build.MODEL
4344

44-
return if (model.startsWith(manufacturer)) {
45+
return if (model.startsWith(manufacturer))
46+
{
4547
capitalizeString(model)
46-
} else {
48+
}
49+
else
50+
{
4751
capitalizeString("$manufacturer $model")
4852
}
4953
}
5054

51-
init {
52-
this.sharedPreferenceData = SharedPreferenceData(context)
53-
55+
init
56+
{
5457
// set api path before using it
5558
// this path will consist of the url which is repeated in every api
5659
// Ex: http://www.example.com/api/
@@ -68,16 +71,21 @@ class ApiServices(context: Context) {
6871
* @param string - string to capitalize
6972
* return - will return the string which was passed in capitalize form
7073
*/
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+
{
7378
return ""
7479
}
7580

7681
val first = string[0]
7782

78-
return if (Character.isUpperCase(first)) {
83+
return if (Character.isUpperCase(first))
84+
{
7985
string
80-
} else {
86+
}
87+
else
88+
{
8189
Character.toUpperCase(first) + string.substring(1)
8290
}
8391
}
@@ -127,11 +135,13 @@ class ApiServices(context: Context) {
127135
*
128136
*
129137
* ***********************************************************************************************
130-
*/
138+
**/
131139
fun makeAPICall(apiName: String, requestMethod: String,
132140
parameters: Boolean, values: JSONObject,
133-
hasToken: Boolean): String? {
134-
try {
141+
hasToken: Boolean): String?
142+
{
143+
try
144+
{
135145
val result: String
136146
val url = URL(apiPath + apiName)
137147

@@ -141,7 +151,8 @@ class ApiServices(context: Context) {
141151
httpURLConnection.addRequestProperty("Content-Type", "application/json; charset=utf-8")
142152
httpURLConnection.addRequestProperty("IsDeviceMode", "1")
143153

144-
if (hasToken) {
154+
if (hasToken)
155+
{
145156
httpURLConnection.addRequestProperty("x-access-token", sharedPreferenceData.getValue("token"))
146157
Log.e(TAG, "makeAPICall: x-access-token is: " + sharedPreferenceData.getValue("token"))
147158
}
@@ -150,7 +161,8 @@ class ApiServices(context: Context) {
150161
httpURLConnection.setRequestProperty("Connection", "close")
151162
Log.e(TAG, "makeAPICall: api = $url values = $values")
152163

153-
if (parameters) {
164+
if (parameters)
165+
{
154166
httpURLConnection.doOutput = true
155167
val outputStream = DataOutputStream(httpURLConnection.outputStream)
156168
outputStream.writeBytes(values.toString())
@@ -162,27 +174,32 @@ class ApiServices(context: Context) {
162174
mResponseCode = responseCode
163175
Log.e(TAG, "makeAPICall: response code from api is: $mResponseCode")
164176

165-
if (responseCode == 200) {
177+
if (responseCode == 200)
178+
{
166179
val bufferedReader = BufferedReader(InputStreamReader(httpURLConnection.inputStream))
167180
val stringBuilder = StringBuilder()
168181
var line: String? = null
169182

170-
while ({line = bufferedReader.readLine(); line}() != null) {
183+
while ({line = bufferedReader.readLine(); line}() != null)
184+
{
171185
stringBuilder.append(line).append("\n")
172186
}
173187

174188
bufferedReader.close()
175189
result = stringBuilder.toString()
176190
Log.e(TAG, "makeAPICall: result from server is: $result")
177-
} else {
191+
}
192+
else
193+
{
178194
val responseMessage = httpURLConnection.responseMessage
179195
Log.e(TAG, "makeAPICall: response message: $responseMessage")
180196

181197
val bufferedReader = BufferedReader(InputStreamReader(httpURLConnection.errorStream))
182198
val stringBuilder = StringBuilder()
183199
var line: String? = null
184200

185-
while ({line = bufferedReader.readLine(); line}() != null) {
201+
while ({line = bufferedReader.readLine(); line}() != null)
202+
{
186203
stringBuilder.append(line).append("\n")
187204
}
188205

@@ -192,12 +209,13 @@ class ApiServices(context: Context) {
192209
}
193210

194211
return result
195-
} catch (e: Exception) {
212+
}
213+
catch (e: Exception)
214+
{
196215
Log.e(TAG, "makeAPICall: in web api services class:\n")
197216
e.printStackTrace()
198217
return null
199218
}
200-
201219
}
202220

203221
/**
@@ -218,12 +236,14 @@ class ApiServices(context: Context) {
218236
* where integer will be the response code
219237
* and bitmap will be the file uploaded.
220238
*/
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+
{
222241
var resultVal: Bitmap? = null
223242
val iStream: InputStream
224243
var responseCode = 0
225244

226-
try {
245+
try
246+
{
227247
val urlConnect = URL(apiPath)
228248
val urlConnection = urlConnect.openConnection() as HttpURLConnection
229249

@@ -236,36 +256,47 @@ class ApiServices(context: Context) {
236256
Log.e(TAG, "getBitmapData: x-access-token is: " + sharedPreferenceData.getValue("token"))
237257
urlConnection.connectTimeout = 30000
238258

239-
if (parameter != null) {
259+
if (parameter != null)
260+
{
240261
urlConnection.doOutput = true
241262
val outputStream = DataOutputStream(urlConnection.outputStream)
242263
outputStream.writeBytes(parameter)
243264
outputStream.flush()
244265
outputStream.close()
245266
}
246267

247-
try {
268+
try
269+
{
248270
responseCode = urlConnection.responseCode
249271
Log.e(TAG, "Response Code from api is -- $responseCode")
250272

251-
if (responseCode == 200) {
273+
if (responseCode == 200)
274+
{
252275
iStream = urlConnection.inputStream
253276

254277
/*Creating a bitmap from the stream returned from the apiPath */
255278
resultVal = BitmapFactory.decodeStream(iStream)
256279
Log.e(TAG, "getBitmapData: result from server is: " + resultVal!!)
257-
} else {
280+
}
281+
else
282+
{
258283
val str = urlConnection.responseMessage
259284
Log.e(TAG, "getBitmapData: response message from api is:$str")
260285
}
261-
} catch (ex: Exception) {
286+
}
287+
catch (ex: Exception)
288+
{
262289
Log.e(TAG, "getBitmapData: exception while downloading images from api:\n")
263290
ex.printStackTrace()
264-
} finally {
291+
}
292+
finally
293+
{
265294
urlConnection.disconnect()
266295
Log.e(TAG, "URL in finally is >$apiPath<-->$resultVal")
267296
}
268-
} catch (e: Exception) {
297+
}
298+
catch (e: Exception)
299+
{
269300
Log.e(TAG, "Exception while geting bitmap:")
270301
e.printStackTrace()
271302
return Pair(responseCode, resultVal)
@@ -274,7 +305,8 @@ class ApiServices(context: Context) {
274305
return Pair(responseCode, resultVal)
275306
}
276307

277-
companion object {
308+
companion object
309+
{
278310
private val TAG = ApiServices::class.java.simpleName
279311
}
280312
}

0 commit comments

Comments
 (0)