fun main() = runBlocking {
val client = HttpClient(CIO) {
install(ContentNegotiation)
install(Logging) {
level = LogLevel.ALL
}
}
val filesToUpload = listOf(
"/path/to/file1.jpg",
"/path/to/file2.pdf"
)
val response: HttpResponse = client.post("https://example.com/upload") {
setBody(
MultiPartFormDataContent(
formData {
filesToUpload.forEachIndexed { index, filePath ->
val file = java.io.File(filePath)
append(
key = "file$index",
value = file.readBytes(),
headers = Headers.build {
append(HttpHeaders.ContentDisposition, "form-data; name=\"file$index\"; filename=\"${file.name}\"")
}
)
}
}
)
)
}
println("Response: ${response.status}, body: ${response.bodyAsText()}")
client.close()
}
===
import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.serialization.kotlinx.json.*
import io.ktor.client.request.forms.*
import kotlinx.coroutines.*
import kotlinx.serialization.*
import java.io.File
// Пример POJO-класса (используем kotlinx.serialization для сериализации)
@Serializable
data class MyPojo(
val id: Int,
val name: String
)
fun main() = runBlocking {
val client = HttpClient(CIO) {
install(ContentNegotiation) {
json() // Подключение JSON-сериализации
}
}
val filesToUpload = listOf(
File("/path/to/file1.jpg"),
File("/path/to/file2.pdf")
)
// Создаем экземпляр POJO
val myPojo = MyPojo(id = 123, name = "Example Name")
val response: HttpResponse = client.post("https://example.com/upload") {
setBody(
MultiPartFormDataContent(
formData {
// Добавляем JSON как часть формы
append(
key = "json",
value = kotlinx.serialization.json.Json.encodeToString(MyPojo.serializer(), myPojo),
headers = Headers.build {
append(HttpHeaders.ContentType, ContentType.Application.Json.toString())
}
)
// Добавляем файлы как части формы
filesToUpload.forEachIndexed { index, file ->
append(
key = "file$index",
value = file.readBytes(),
headers = Headers.build {
append(HttpHeaders.ContentDisposition, "form-data; name=\"file$index\"; filename=\"${file.name}\"")
}
)
}
}
)
)
}
println("Response: ${response.status}, body: ${response.bodyAsText()}")
client.close()
}
====
fun main() = runBlocking {
val client = HttpClient(CIO) {
install(ContentNegotiation) {
json()
}
}
val filesToUpload = listOf(
File("/path/to/largefile1.mp4"),
File("/path/to/largefile2.zip")
)
val myPojo = MyPojo(id = 123, name = "Example Data")
val response: HttpResponse = client.post("https://example.com/upload") {
setBody(
MultiPartFormDataContent(
formData {
// Добавляем JSON как часть формы
append(
key = "json",
value = kotlinx.serialization.json.Json.encodeToString(MyPojo.serializer(), myPojo),
headers = Headers.build {
append(HttpHeaders.ContentType, ContentType.Application.Json.toString())
}
)
// Добавляем файлы с использованием потоков
filesToUpload.forEachIndexed { index, file ->
appendInput(
key = "file$index",
headers = Headers.build {
append(HttpHeaders.ContentDisposition, "form-data; name=\"file$index\"; filename=\"${file.name}\"")
},
size = file.length()
) {
file.inputStream() // Поток файла
}
}
}
)
)
}
println("Response: ${response.status}, body: ${response.bodyAsText()}")
client.close()
}
===
val response: HttpResponse = client.post("https://example.com/upload") {
setBody(
MultiPartFormDataContent(
formData {
// Добавляем JSON как часть формы
append(
key = "json",
value = kotlinx.serialization.json.Json.encodeToString(MyPojo.serializer(), myPojo),
headers = Headers.build {
append(HttpHeaders.ContentType, ContentType.Application.Json.toString())
}
)
// Добавляем файлы с использованием массива ключей files[]
filesToUpload.forEach { file ->
appendInput(
key = "files[]", // Массив файлов с ключом files[]
headers = Headers.build {
append(HttpHeaders.ContentDisposition, "form-data; name=\"files[]\"; filename=\"${file.name}\"")
},
size = file.length()
) {
file.inputStream()
}
}
}
)
)
}
===
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Чтение JSON
$json = json_decode($_POST['json'], true);
print_r($json); // Данные JSON
// Обработка файлов
foreach ($_FILES['files']['name'] as $index => $name) {
$tmpName = $_FILES['files']['tmp_name'][$index];
$size = $_FILES['files']['size'][$index];
$error = $_FILES['files']['error'][$index];
if ($error === UPLOAD_ERR_OK) {
$destination = "uploads/" . basename($name);
move_uploaded_file($tmpName, $destination);
echo "Файл $name успешно загружен в $destination\n";
} else {
echo "Ошибка при загрузке файла $name\n";
}
}
}
?>
===
передача JSON в теле запроса (body) несовместима с форматом multipart/form-data
===
Если сервер поддерживает multipart/form-data
"""
Speech to Text
Speech to Text
ACTION_RECOGNIZE_SPEECH
private const val BASE_URL = "https://jsonplaceholder.typicode.com/"
private const val TIME_OUT = 6000
suspend fun makeRequest() {
val client = HttpClient(Android) {
install(Logging) {
logger = object : Logger {
override fun log(message: String) {
Log.d("@@@:", message)
}
}
}
engine {
// this: AndroidEngineConfig
connectTimeout = 100_000
socketTimeout = 100_000
//proxy = Proxy(Proxy.Type.HTTP, InetSocketAddress("localhost", 8080))
}
}
val client0 = HttpClient(CIO) {
}
val response: HttpResponse = client.request("https://ktor.io/") {
// Configure request parameters exposed by HttpRequestBuilder
}
println("======"+response.bodyAsText())
}
=====
dependencies {
implementation "androidx.compose.material:material-icons-extended:$compose_ui_version"
}
Step 3: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package com.geeksforgeeks.passwordtoggle
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Visibility
import androidx.compose.material.icons.filled.VisibilityOff
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
// Creating a Simple Scaffold
// Layout for the application
Scaffold(
// Creating a Top Bar
topBar = { TopAppBar(title = { Text("GFG | Toggle Password", color = Color.White) }, backgroundColor = Color(0xff0f9d58)) },
// Creating Content
content = {
// Creating a Column Layout
Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) {
// Creating a variable to store password
var password by remember { mutableStateOf("") }
// Creating a variable to store toggle state
var passwordVisible by remember { mutableStateOf(false) }
// Create a Text Field for giving password input
TextField(
value = password,
onValueChange = { password = it },
label = { Text("Password") },
singleLine = true,
placeholder = { Text("Password") },
visualTransformation = if (passwordVisible) VisualTransformation.None else PasswordVisualTransformation(),
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
trailingIcon = {
val image = if (passwordVisible)
Icons.Filled.Visibility
else Icons.Filled.VisibilityOff
// Localized description for accessibility services
val description = if (passwordVisible) "Hide password" else "Show password"
// Toggle button to hide or display password
IconButton(onClick = {passwordVisible = !passwordVisible}){
Icon(imageVector = image, description)
}
}
)
}
}
)
}
}
}
/*
Note: to use Icons.Filled.Visibility and Icons.Filled.VisibilityOff
add in the dependencies: implementation "androidx.compose.material:material-icons-extended:$compose_version"
*/
===
https://www.youtube.com/watch?app=desktop&v=4MJFmhcONfI&ab_channel=PhilippLackner
implementation "androidx.compose.material:material-icons-extended:1.3.1"
https://medium.com/google-developer-experts/how-to-create-a-composable-password-with-jetpack-compose-f1be2d48d9f0
android - ktor
Подписаться на:
Комментарии к сообщению (Atom)
Комментариев нет:
Отправить комментарий