0

I need to send http get request in andoid app.

...
import java.net.HttpURLConnection
import java.net.URL

...

fun sendGet(view: View) {
        val url = URL("http://www.google.com/")

        with(url.openConnection() as HttpURLConnection) {
            requestMethod = "GET"  // optional default is GET

            println("\nSent 'GET' request to URL : $url; Response Code : $responseCode")

            inputStream.bufferedReader().use {
                it.lines().forEach { line ->
                    println(line)
                }
            }
        }
    }
}

Manifest

  <uses-permission android:name="android.permission.INTERNET" />
    <application

Then i press send request button app crashes.

Logs

2020-06-08 13:37:58.832 7039-7039/com.example.remote E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.remote, PID: 7039 java.lang.IllegalStateException: Could not execute method for android:onClick at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:402) at android.view.View.performClick(View.java:7357) at android.view.View.performClickInternal(View.java:7334) at android.view.View.access$3600(View.java:808) at android.view.View$PerformClick.run(View.java:28200) at android.os.Handler.handleCallback(Handler.java:907) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7478) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397) at android.view.View.performClick(View.java:7357)  at android.view.View.performClickInternal(View.java:7334)  at android.view.View.access$3600(View.java:808)  at android.view.View$PerformClick.run(View.java:28200)  at android.os.Handler.handleCallback(Handler.java:907)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loop(Looper.java:223)  at android.app.ActivityThread.main(ActivityThread.java:7478)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)  Caused by: java.io.IOException: Cleartext HTTP traffic to www.google.com not permitted at com.android.okhttp.HttpHandler$CleartextURLFilter.checkURLPermitted(HttpHandler.java:127) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:462) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:411) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:542) at com.example.remote.MainActivity.sendGet(MainActivity.kt:24) at java.lang.reflect.Method.invoke(Native Method)  at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)  at android.view.View.performClick(View.java:7357)  at android.view.View.performClickInternal(View.java:7334)  at android.view.View.access$3600(View.java:808)  at android.view.View$PerformClick.run(View.java:28200)  at android.os.Handler.handleCallback(Handler.java:907)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loop(Looper.java:223)  at android.app.ActivityThread.main(ActivityThread.java:7478)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941) 

2

1 Answer 1

2

Try This,

fun sendGet() {
        try {
            //disable the strict mode otherwise perform this operation on netWork Thread
            val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
            StrictMode.setThreadPolicy(policy)

            // GET Request
            val request = "http://www.google.com/"
            val url = URL(request)
            val conn = url.openConnection()
            conn.doOutput = true
            // Get the response
            val rd = BufferedReader(InputStreamReader(conn.getInputStream()))
            var line: String
            var sResult = ""
            while (rd.readLine().also { line = it } != null) {
                // Process line...
                sResult = "$sResult$line "
            }
            rd.close()
            Log.e("RESULT", sResult)
        } catch (e: Exception) {
            println("Error $e")
        }
    }
Sign up to request clarification or add additional context in comments.

5 Comments

2020-06-08 21:20:03.696 16271-16271/com.example.remote I/System.out: Error java.io.FileNotFoundException: google.com
Paste your GET request URL properly instead of google.com
Thanks. I ve tried to send a request to iplogger. It is working !
LOVE YOU! You have made my day)
you're welcome. Please, also do upvote.. if you're problem has been solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.