I'm trying to send an Android edittext in JSON form to display the data on a website. This has to work simultaneously as the user clicks a button in the app. I've looked all around stackoverflow and youtube but haven't found a way to do so. The part I'm having trouble is how to send the data to url and how to retrieve it using javascript. I would just like guidance on what to use and how to do so. Thank you!
I've tried to use okhttp and retrofit, but I've never done any work like this or have used JSON so have gotten confused and have took out all the code.
For reference:
This is mainactivity from app:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener() {
val inputValue: String? = input_area.text.toString()
if (inputValue == null || inputValue.trim() == "") {
//if (inputValue.trim() == "") {
Toast.makeText(this, "Please input data", Toast.LENGTH_LONG).show()
}
else {
val toSend= JSONObject()
toSend.put("input", inputValue)
}
}
}
private inner class Callback : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
return false
}
}
}
This is my html for where I need to receive the data:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
<p id="demo"></p>
<script>
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var myObj = JSON.parse(this.responseText);
document.getElementById("demo").innerHTML = myObj.input;
}
};
xmlhttp.open("GET", "", true);
xmlhttp.send();
</script>
</body>
</html>
Aucun commentaire:
Enregistrer un commentaire