Last active
May 5, 2020 13:09
-
-
Save lucnicolas/730f03dfae8194ddc1af718f2f96f6b8 to your computer and use it in GitHub Desktop.
Android - HTTP POST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml ... > | |
<manifest ... > | |
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> | |
<uses-permission android:name="android.permission.INTERNET" /> | |
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | |
<application ... /> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CallAPI extends AsyncTask<String, String, String> { | |
public CallAPI() { | |
//set context variables if required | |
} | |
@Override | |
protected void onPreExecute() { | |
super.onPreExecute(); | |
} | |
@Override | |
protected String doInBackground(String... params) { | |
String urlString = params[0]; // URL to call | |
String data = "data=" + params[1]; //data to post | |
OutputStream out = null; | |
try { | |
URL url = new URL(urlString); | |
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); | |
urlConnection.setDoOutput(true); | |
//urlConnection.setRequestMethod("POST"); | |
out = new BufferedOutputStream(urlConnection.getOutputStream()); | |
// Send POST data request | |
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8)); | |
writer.write(data); | |
writer.flush(); | |
writer.close(); | |
out.close(); | |
// Get the server response | |
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); | |
StringBuilder sb = new StringBuilder(); | |
String line = null; | |
// Read server response | |
while ((line = reader.readLine()) != null) { | |
//Append server response in string | |
sb.append(line + "\n"); | |
} | |
String text = sb.toString(); | |
Log.d("SERVER RESPONSE", text); | |
//urlConnection.connect(); | |
} catch (Exception e) { | |
System.out.println(e.getMessage()); | |
} | |
return urlString; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ($_POST["data"]) { | |
$data = $_POST["data"].PHP_EOL; | |
$fp = fopen("db", "a"); | |
fwrite($fp, $data); | |
print "Data : $data"; | |
} else print | |
<<<TAG | |
<form action="index.php" method="post"> | |
TEST : <input type="text" name="data" /> | |
<input type="submit" value="submit" /> | |
</form> | |
TAG; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.home_layout); | |
callAPI.execute("http://192.168.1.1/", "Data from android"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment