Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class httpPost {
- private String response = null;
- private String URL = "https://mio url";
- public String invio() {
- StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
- StrictMode.setThreadPolicy(policy);
- try {
- URL url = new URL(URL);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("POST");
- conn.setDoInput(true);
- conn.setDoOutput(true);
- Uri.Builder builder = new Uri.Builder();
- //Parametri da passare
- builder.appendQueryParameter("chiave1", "valore1");
- builder.appendQueryParameter("chiave2", "valore2");
- builder.appendQueryParameter("chiave3", "valore3");
- builder.appendQueryParameter("chiave4", "valore4");
- String query = builder.build().getEncodedQuery();
- OutputStream os = conn.getOutputStream();
- BufferedWriter writer = new BufferedWriter(
- new OutputStreamWriter(os, StandardCharsets.UTF_8));
- writer.write(query);
- writer.flush();
- writer.close();
- InputStream in = new BufferedInputStream(conn.getInputStream());
- response = http.InputStreamToString(in);
- } catch (MalformedURLException e) {
- Log.e("HTTP", "MalformedURLException: " + e.getMessage());
- } catch (ProtocolException e) {
- Log.e("HTTP", "ProtocolException: " + e.getMessage());
- } catch (IOException e) {
- Log.e("HTTP", "IOException: " + e.getMessage());
- } catch (Exception e) {
- Log.e("HTTP", "Exception: " + e.getMessage());
- }
- return response;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement