Отправка POST-запроса из приложения Android на веб-сайт ⇐ JAVA
-
Anonymous
Отправка POST-запроса из приложения Android на веб-сайт
I want to display data sent from an android app using POST request to a simple html website whenever a button in the app is pressed. I'm hosting the website and the php file to handle the POST in xampp apache, I've set up the server so the app on my smartphone can access it via local ip and port (192.168.x.xx:8080), but I keep getting the 404 error not found. Can you guys help me?
The PHP file to handle the POST request:
The HTML code:
function fetchMessage() { fetch('message_handler.php') .then(response => response.text()) .then(messageHtml => { document.getElementById('messageContainer').innerHTML = messageHtml; }) .catch(error => console.error('Error fetching message:', error)); } fetchMessage(); setInterval(fetchMessage, 5000); The java class to send the POST request:
import android.os.AsyncTask; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; public class SendMessage { public void sendMessage() { new SendMessageTask().execute(); } private class SendMessageTask extends AsyncTask { @Override protected Void doInBackground(Void... voids) { try { URL url = new URL("http://192.168.xx.xx:8080/siteteste/message_handler.php"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("POST"); con.setDoOutput(true); con.setRequestProperty("Content-Type", "application/json"); String message = "Atenção: O operador X do ponto Y do setor Z precisa de suporte!"; try (OutputStream os = con.getOutputStream()) { byte[] input = message.getBytes("utf-8"); os.write(input, 0, input.length); } int responseCode = con.getResponseCode(); System.out.println("Response code: " + responseCode); } catch (Exception e) { e.printStackTrace(); } return null; } } } Whenever I press the button which sends the request, I get a 404 error as the responseCode, but when I check the logs on xampp files, I can see that the device I'm running the app has access to the server
192.168.xx.xx - - [05/Mar/2024:00:05:36 -0300] "POST /siteteste/message_handler.php HTTP/1.1" 404 9 "-" "Dalvik/2.1.0 (Linux; U; Android 12; SM-G780G Build/SP1A.210812.016)"
Also I get this error everytime the fetchMessage() function update when I open the website on the browser (Chrome DevTools) siteteste.html:13 GET http://192.168.xx.xx:8080/siteteste/message_handler.php 404 (Not Found)
What am I doing wrong? Thanks a lot for the help and sorry for any mistakes, english is not my first language.
Источник: https://stackoverflow.com/questions/781 ... to-website
I want to display data sent from an android app using POST request to a simple html website whenever a button in the app is pressed. I'm hosting the website and the php file to handle the POST in xampp apache, I've set up the server so the app on my smartphone can access it via local ip and port (192.168.x.xx:8080), but I keep getting the 404 error not found. Can you guys help me?
The PHP file to handle the POST request:
The HTML code:
function fetchMessage() { fetch('message_handler.php') .then(response => response.text()) .then(messageHtml => { document.getElementById('messageContainer').innerHTML = messageHtml; }) .catch(error => console.error('Error fetching message:', error)); } fetchMessage(); setInterval(fetchMessage, 5000); The java class to send the POST request:
import android.os.AsyncTask; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; public class SendMessage { public void sendMessage() { new SendMessageTask().execute(); } private class SendMessageTask extends AsyncTask { @Override protected Void doInBackground(Void... voids) { try { URL url = new URL("http://192.168.xx.xx:8080/siteteste/message_handler.php"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("POST"); con.setDoOutput(true); con.setRequestProperty("Content-Type", "application/json"); String message = "Atenção: O operador X do ponto Y do setor Z precisa de suporte!"; try (OutputStream os = con.getOutputStream()) { byte[] input = message.getBytes("utf-8"); os.write(input, 0, input.length); } int responseCode = con.getResponseCode(); System.out.println("Response code: " + responseCode); } catch (Exception e) { e.printStackTrace(); } return null; } } } Whenever I press the button which sends the request, I get a 404 error as the responseCode, but when I check the logs on xampp files, I can see that the device I'm running the app has access to the server
192.168.xx.xx - - [05/Mar/2024:00:05:36 -0300] "POST /siteteste/message_handler.php HTTP/1.1" 404 9 "-" "Dalvik/2.1.0 (Linux; U; Android 12; SM-G780G Build/SP1A.210812.016)"
Also I get this error everytime the fetchMessage() function update when I open the website on the browser (Chrome DevTools) siteteste.html:13 GET http://192.168.xx.xx:8080/siteteste/message_handler.php 404 (Not Found)
What am I doing wrong? Thanks a lot for the help and sorry for any mistakes, english is not my first language.
Источник: https://stackoverflow.com/questions/781 ... to-website
Мобильная версия