|
| 1 | +/* |
| 2 | + Copyright 2016 Parse.ly, Inc. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | +@file:Suppress("DEPRECATION") |
| 17 | +package com.parsely.parselyandroid |
| 18 | + |
| 19 | +import android.os.AsyncTask |
| 20 | +import java.net.HttpURLConnection |
| 21 | +import java.net.URL |
| 22 | + |
| 23 | +internal class ParselyAPIConnection(private val tracker: ParselyTracker) : AsyncTask<String?, Exception?, Void?>() { |
| 24 | + private var exception: Exception? = null |
| 25 | + |
| 26 | + @Deprecated("Deprecated in Java") |
| 27 | + override fun doInBackground(vararg data: String?): Void? { |
| 28 | + var connection: HttpURLConnection? = null |
| 29 | + try { |
| 30 | + if (data.size == 1) { // non-batched (since no post data is included) |
| 31 | + connection = URL(data[0]).openConnection() as HttpURLConnection |
| 32 | + connection.inputStream |
| 33 | + } else if (data.size == 2) { // batched (post data included) |
| 34 | + connection = URL(data[0]).openConnection() as HttpURLConnection |
| 35 | + connection.doOutput = true // Triggers POST (aka silliest interface ever) |
| 36 | + connection.setRequestProperty("Content-Type", "application/json") |
| 37 | + val output = connection.outputStream |
| 38 | + output.write(data[1]?.toByteArray()) |
| 39 | + output.close() |
| 40 | + connection.inputStream |
| 41 | + } |
| 42 | + } catch (ex: Exception) { |
| 43 | + exception = ex |
| 44 | + } |
| 45 | + return null |
| 46 | + } |
| 47 | + |
| 48 | + @Deprecated("Deprecated in Java") |
| 49 | + override fun onPostExecute(result: Void?) { |
| 50 | + if (exception != null) { |
| 51 | + ParselyTracker.PLog("Pixel request exception") |
| 52 | + ParselyTracker.PLog(exception.toString()) |
| 53 | + } else { |
| 54 | + ParselyTracker.PLog("Pixel request success") |
| 55 | + |
| 56 | + // only purge the queue if the request was successful |
| 57 | + tracker.purgeEventsQueue() |
| 58 | + ParselyTracker.PLog("Event queue empty, flush timer cleared.") |
| 59 | + tracker.stopFlushTimer() |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments