battle programming a forum for elite programmers with extreme will power to sharpen theire skills
 
HomeCalendarFAQSearchMemberlistUsergroupsRegisterLog in
Share
 

 kotlin AS PL grimoire

Go down 
Go to page : Previous  1, 2
AuthorMessage
kurosen

kurosen

Posts : 102
Join date : 2012-04-17

kotlin AS PL grimoire - Page 2 Empty
PostSubject: Kotlin get url source code as text   kotlin AS PL grimoire - Page 2 EmptyFri Jun 28, 2019 11:31 pm


Code:
package com.example.json3

import android.os.AsyncTask
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import org.json.JSONObject
import java.io.IOException
import java.io.InputStreamReader
import java.lang.Exception
import java.net.HttpURLConnection
import java.net.URL
import android.widget.Toast
import java.io.BufferedReader


class MainActivity : AppCompatActivity() {


    private var xmlString = ""
    private val XML_URL = "https://www.yotamarker.com"
    var XML_UR= "https://reqres.in/api/users/2"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        getDataJSON()
        try {
            Thread.sleep(6000)
        } catch (e: InterruptedException) {
            e.printStackTrace()
        }

        txt1.text = xmlString


    }

    fun getDataJSON() {
        //we never use void in AsyncTask, we need to use Void
        object : AsyncTask<Void, Void, String>() {
            override fun doInBackground(vararg voids: Void): String {
                //we need to open HTTP URL Connection to our desired URL (www.boi.org.il)
                var connection: HttpURLConnection? = null
                try {
                    connection = URL(XML_URL).openConnection() as HttpURLConnection
                    val buf = BufferedReader(InputStreamReader(connection.inputStream))
                    var line: String?
                    line = buf.readLine()
                    while (line != null) {
                        line = buf.readLine()
                        xmlString += line
                    }
                } catch (e: IOException) {
                    e.printStackTrace()
                } finally {
                    connection!!.disconnect()
                }

                return xmlString
            }

            override fun onPostExecute(jsonString: String) {
                super.onPostExecute(jsonString)
                http@ http://Log.e("XML", "onPostExecute: " + jsonString);
                // display json string
                Toast.makeText(applicationContext, jsonString, Toast.LENGTH_LONG).show()

            }
        }.execute()
    }

}

in the manifest :
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

chobit
Back to top Go down
View user profile
kurosen

kurosen

Posts : 102
Join date : 2012-04-17

kotlin AS PL grimoire - Page 2 Empty
PostSubject: kotlin getting JSON info   kotlin AS PL grimoire - Page 2 EmptySat Jun 29, 2019 12:51 am

here I used the swap api to get Luke Skywalker :

here is the swap if you want to play : https://swapi.co/

txt1 is the default textbook of the kotlin empty main activity
add internet permission to the manifest

Code:
package com.example.json3

import android.os.AsyncTask
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import org.json.JSONObject
import java.io.IOException
import java.io.InputStreamReader
import java.lang.Exception
import java.net.HttpURLConnection
import java.net.URL
import android.widget.Toast
import java.io.BufferedReader
import org.json.JSONException




class MainActivity : AppCompatActivity() {


    private var jsonString = ""
    private val jsonURL = "https://swapi.co/api/people/1/"
    //var XML_UR= "https://reqres.in/api/users/2"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        getDataJSON()
        try {
            Thread.sleep(6000)
        } catch (e: InterruptedException) {
            e.printStackTrace()
        }

        http://txt1.text = jsonString
        parseJson(jsonString)

    }

    fun getDataJSON() {
        //we never use void in AsyncTask, we need to use Void
        object : AsyncTask<Void, Void, String>() {
            override fun doInBackground(vararg voids: Void): String {
                //we need to open HTTP URL Connection to our desired URL (www.boi.org.il)
                var connection: HttpURLConnection? = null
                try {
                    connection = URL(jsonURL).openConnection() as HttpURLConnection
                    val buf = BufferedReader(InputStreamReader(connection.inputStream))
                    var line: String? = buf.readLine()
                    while (line != null) {
                        jsonString += line
                    }
                } catch (e: IOException) {
                    e.printStackTrace()
                } finally {
                    connection!!.disconnect()
                }

                return jsonString
            }

            override fun onPostExecute(jsonString: String) {
                super.onPostExecute(jsonString)
                http://Log.e("XML", "onPostExecute: $jsonString")
                parseJson(jsonString)
            }
        }.execute()
    }

    private fun parseJson(jsonString: String) {
        val jsonObj = JSONObject(jsonString)
        val namee = jsonObj.getString("name")

        txt1.text= namee

    }

}
Back to top Go down
View user profile
Moti Barski

Moti Barski

Posts : 330
Join date : 2011-08-02

kotlin AS PL grimoire - Page 2 Empty
PostSubject: kotlin get battery data   kotlin AS PL grimoire - Page 2 EmptyWed Jul 10, 2019 7:15 pm

it shows number of times displayed + battery level
also notice in the onReceive interface there are vals that can show
if charging, and by AC or usb charging

Code:
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    var b8TRcounter = 0
    private val mBatInfoReceiver = object : BroadcastReceiver() {
        override fun onReceive(ctxt: Context, intent: Intent) {
            val level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)
            b8TRcounter++;
            txtBox.setText("$level% $b8TRcounter")
            val status: Int = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1)
            val isCharging: Boolean = status == BatteryManager.BATTERY_STATUS_CHARGING
                    || status == BatteryManager.BATTERY_STATUS_FULL

            val chargePlug: Int = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1)
            val usbCharge: Boolean = chargePlug == BatteryManager.BATTERY_PLUGGED_USB
            val acCharge: Boolean = chargePlug == BatteryManager.BATTERY_PLUGGED_AC
            //txtBox.setText("$usbCharge")
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        this.registerReceiver(this.mBatInfoReceiver, IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    }

}



nice cheers

_________________
MB over and out emp it up
Back to top Go down
View user profile
 
kotlin AS PL grimoire
Back to top 
Page 2 of 2Go to page : Previous  1, 2

Permissions in this forum:You cannot reply to topics in this forum
battle programmers alliance :: battle programming alliance :: battle programming-
Jump to: