В моем приложении есть кнопка с надписью «Готово», и когда эта кнопка нажата, она отправляет запрос POST на сервер и должен отображать текст ответа под кнопка. Вот упрощенная версия моего кода:
Код: Выделить всё
// ...imports and other code...
const TypeScreen = () => {
const [mole, setMole] = useState('');
const [responseText, setResponseText] = useState('');
const sendPostRequest = async () => {
try {
const ipAddress = '/*IP*/';
const port = '/*PORT*/';
const endpoint = `http://${ipAddress}:${port}/`;
const requestBody = {
"key1": mole
};
const json = JSON.stringify(requestBody);
const response = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: json,
});
if (response.ok) {
const responseText = await response.text();
console.log('POST request successful');
console.log('Response Text: ', responseText);
setResponseText(responseText);
} else {
console.error('POST request failed with status:', response.status);
}
} catch (error) {
console.error('Error sending POST request:', error);
}
};
return (
// ...other components and UI...
Done
Response:
{responseText}
);
}
Подробнее здесь: https://stackoverflow.com/questions/772 ... -build-apk
Мобильная версия