Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android/Kotlin:使用URLConnection发送数据时出现问题_Android_Kotlin_Httpsurlconnection - Fatal编程技术网

Android/Kotlin:使用URLConnection发送数据时出现问题

Android/Kotlin:使用URLConnection发送数据时出现问题,android,kotlin,httpsurlconnection,Android,Kotlin,Httpsurlconnection,我正在尝试将数据从android应用程序的活动安全地发送到远程服务器上的php脚本。当我启动应用程序时,应用程序不会崩溃,也不会在控制台中提示我任何错误消息。但是,数据似乎没有发送到php脚本 我试图通过在每行之间添加Log.d(…)命令来缩小可能发生错误的行数。当我在调试模式下运行应用程序时,每个Log.d(…)都会打印到控制台,因此我认为php脚本不正确。但是,进一步的调查显示,数据没有到达php脚本,因为$score=$\u POST[“score”]不包含预期值。另外,如果我任意更改请求

我正在尝试将数据从android应用程序的活动安全地发送到远程服务器上的php脚本。当我启动应用程序时,应用程序不会崩溃,也不会在控制台中提示我任何错误消息。但是,数据似乎没有发送到php脚本

我试图通过在每行之间添加Log.d(…)命令来缩小可能发生错误的行数。当我在调试模式下运行应用程序时,每个Log.d(…)都会打印到控制台,因此我认为php脚本不正确。但是,进一步的调查显示,数据没有到达php脚本,因为$score=$\u POST[“score”]不包含预期值。另外,如果我任意更改请求的url并再次启动应用程序,也不会出现错误消息。因此,我认为错误位于MyActivity.kt文件中的url.openConnection()和urlConnection.connect()之间

问题

为什么代码不将数据发送到脚本,我如何修复它

类MyActivity:AppCompatActivity(){
重写创建时的乐趣(savedInstanceState:Bundle?){
...
此文件为.configureNetworkRunnable()
}
private fun configureNetworkRunnable(){
线程(可运行{
上传数据()
}).start()
}
私人娱乐上传数据(){
val charset=“UTF-8”
val参数=URLEncoder.encode(“参数1”,字符集)+“=”+URLEncoder.encode(“20”,字符集)
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


由于该URL的协议是“https”,所以将该URL连接强制转换为HttpsURLConnection。然后,因为我们正在发送数据,所以将请求方法设置为“POST”。我们还将设置要发送的数据的内容类型,以便脚本知道预期的内容。因此,该HttpsURLConnection的构造应如下所示:

val urlConnection=url.openConnection()作为HttpsURLConnection
urlConnection.apply{
requestMethod=“POST”
doOutput=true
setRequestProperty(“内容类型”、“应用程序/八位字节流”)
}
在发送数据后,看起来您也没有正确地关闭流。尝试:

urlConnection.outputStream.use{stream->
stream.write(“20.toByteArray(Charsets.UTF_8))
}
它将把字节写入您的流,并为您关闭它,不管是否异常

最后,您必须完全读取inputStream,因此请尝试:

urlConnection.inputStream.bufferedReader()。使用{
it.readText()
}

如果您仍然遇到问题,请打印以记录您的
urlConnection.responseCode
,并为我选择
it.readText()

谢谢!我已经应用了你建议的代码``ìt.readText()``打印我的回音。因此,连接已建立,但似乎值传输不起作用,因为php文件中的param1为0。响应代码:200。此处必须读取inputStream,还是仅用于调试目的?提前谢谢你@ingoBe我对PHP不太熟悉,但我们在这里编写请求正文中的字节(不是作为查询参数的值),因此请尝试使用以读取PHP脚本中的POST正文。请确保在脚本中使用正确的字符集/编码将字节转换回字符串。此外,据我回忆,在通过URLConnection发送数据时,您必须完全读取输入流。这不仅仅是为了调试。编辑:啊,对不起,我知道你现在是如何尝试使用
参数的。您发送的数据是否始终是字符串?或者它在任何时候都可以是任意的二进制数据?谢谢!到目前为止,二进制数据是可能的,但稍后我可能还必须发送字符串。这就是为什么我从一开始就这么做。
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
D/OpenGLRenderer: makeCurrent EglSurface : 0x7cf3783000 -> 0x7cf3783b00
D/ViewRootImpl@1b125bd[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/ViewRootImpl@fc1415c[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/ViewRootImpl@fc1415c[MainActivity]: stopped(true) old=false
V/ViewRootImpl@fc1415c[MainActivity]: updateAppliedLetterboxDirection, direction=0, Caller=android.view.ViewRootImpl.handleDispatchLetterboxDirectionChanged:8044