Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] Android/Kotlin: Problems when sending data with URLConnection

Posted on November 14, 2022

Solution 1 :

As the protocol of that URL is “https”, cast that URLConnection to HttpsURLConnection. Then, since we’re sending data, set the request method to “POST”. We’ll also set the content-type of the data we’re sending so our script knows what to expect. So the construction of this HttpsURLConnection should look like:

val urlConnection = url.openConnection() as HttpsURLConnection
urlConnection.apply {
  requestMethod = "POST"
  doOutput = true
  setRequestProperty("Content-Type", "application/octet-stream")
}

It also doesn’t look like you’re properly closing your stream after sending the data. Try:

urlConnection.outputStream.use { stream ->
  stream.write("20".toByteArray(Charsets.UTF_8))
}

which will write the bytes to your stream and handle closing it for you, exception or not.

Finally, you have to read the inputStream fully, so try:

urlConnection.inputStream.bufferedReader().use {
  it.readText()
}

If you’re still experiencing issues, print to log your urlConnection.responseCode and optionally that it.readText() for me.

Problem :

I am trying to securely send data from an activity of my android application to a php script on a remote server. When I start the application, the application is not crashing nor am I prompted any error message in the console. However the data do not seem to be sent to the php script.

I have tried to narrow down the lines where the error could occur by adding Log.d(…) commands between each line. When I run the application in debug mode, every Log.d(…) is printed to the console, so I expected the php script to be incorrect. However, further investigation has shown the data do not arrive at the php script, since $score = $_POST[“score”] is not containing the expected value. Also, if I arbitrarily change the url for the request and start the application again, no error message occurs either. So I expect that the error lies somewhere between url.openConnection() and urlConnection.connect() in the MyActivity.kt file.

READ  [ANSWERED] android - imageCapture.takePicture - saved image convert to Bitmap
Powered by Inline Related Posts

Question

Why is the code not sending the data to the script and how can I fix it?

class MyActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        ...
        this.configureNetworkRunnable()
    }
   
    private fun configureNetworkRunnable() {
        Thread( Runnable {
            uploadData()
        }).start()
    }

    private fun uploadData() {
        val charset = "UTF-8"
        val parameter = URLEncoder.encode("param1", charset) + "=" + URLEncoder.encode("20", charset)

        val url = URL("https://www.myurl.com/path/file.php")
        val urlConnection = url.openConnection()
        urlConnection.setDoOutput(true)

        val outputStream: OutputStream = urlConnection.getOutputStream()
        outputStream.write(parameter.toByteArray(Charsets.UTF_8))
        urlConnection.connect()
    }
}

GameActivity.kt

<?php
    $param1 = $_POST["param1"];
    ...

file.php

D/NetworkSecurityConfig: No Network Security Config specified, using platform default
D/OpenGLRenderer: makeCurrent EglSurface : 0x7cf3783000 -> 0x7cf3783b00
D/[email protected][GameActivity]: MSG_RESIZED: frame=(112,0,2280,1080) ci=(0,0,0,0) vi=(0,0,0,0) or=2
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
D/TcpOptimizer: TcpOptimizer-ON
D/libmdf: libmdf v2.9.0.0 On 64bit PLATFORM
D/OpenGLRenderer: destroyEglSurface : 0x7cf3783000
I/mali_egl: eglDestroySurface() in
I/mali_winsys: delete_surface() [2168x1080] return
I/mali_egl: eglDestroySurface() out
W/libEGL: EGLNativeWindowType 0x7d82347810 disconnect failed
D/OpenGLRenderer: ~ReliableSurface : 0x7d82347800
D/[email protected][MainActivity]: Relayout returned: old=(112,0,2280,1080) new=(112,0,2280,1080) req=(2168,1080)8 dur=10 res=0x5 s={false 0} ch=true
D/[email protected][MainActivity]: stopped(true) old=false
V/[email protected][MainActivity]: updateAppliedLetterboxDirection, direction=0, Caller=android.view.ViewRootImpl.handleDispatchLetterboxDirectionChanged:8044

console

Thank you in advance.

Comments

Comment posted by ingoBe

Thank you! I have applied your suggested code. “ìt.readText()“` prints my echo. So, the connection is established, but it seems, that the value transfer doesn’t work, because the param1 from the php file is 0. responseCode: 200. Is it mandatory to read the inputStream here, or is it only for debugging purposes? Thank you in advance!

Comment posted by this answer

@ingoBe I’m not too familiar with PHP, but we’re writing the bytes in the request body here (not as a value to a query param) so try using

Comment posted by stackoverflow.com/a/4844926/14032472

Also, from my recollection, you HAVE to read the input stream fully when sending data through a URLConnection. It’s not just for debugging purposes.

READ  [ANSWERED] javascript - React Native Expo SDK version is outdated
Powered by Inline Related Posts

Comment posted by kabumere

Ah, sorry, I’m seeing how you were trying to use

Comment posted by ingoBe

Thanks! Up to now binary data would be possible, but later I probably have to send strings as well. That is why I did it from the beginning.

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress