I've been looking for methods to do send data to a web form or a web service, principally php servers, but there's a lot of answers with deprecated methods, I have to post a single value in a form, and another more elaborated webservice. So there are like 3 questions:
- I want to know if is posible to post data in a form and submit it, and if is possible how to do that. The form isn't a webservice, is a form to user's input (this one).
- How to use correctly the HttpUrlConnection to call a webservice, (post data and get response), the webservice can use JSON or simple 2 string values like user and password.
Here's the code that I've found, basically:
private class PostTask extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... params) {
HttpURLConnection conn=null;
try {
URL url = new URL(params[0]);
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
//conn.setReadTimeout(10000);
//conn.setConnectTimeout(15000);
//conn.setDoInput(true);
String body = "4";
conn.connect();
OutputStream output = new BufferedOutputStream(conn.getOutputStream());
output.write(body.getBytes());
output.flush();
} catch (Exception e) {
e.printStackTrace();
}finally {
assert conn != null;
conn.disconnect();
}
return true;
}
}