1

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;
    }
}
Brandon Zamudio
  • 2,853
  • 4
  • 18
  • 34
  • Have you checked about `retrofit`? I think is the most easiest way to manage server calls, like POST, PUT, DELETE, and it already have an abstraction for Http classes. – diogojme Dec 21 '15 at 19:22
  • No I'm not, is a library? – Brandon Zamudio Dec 21 '15 at 19:25
  • Yes, it is. Is easy do these calls to your server and it's based on RestFULL pattern, you will find a lot of samples in the internet. You can check it from the source http://square.github.io/retrofit/. – diogojme Dec 21 '15 at 19:31
  • Right but I can't compile another library, especially if I can do it without so much problem, I'm already compiling 5 libraries, if I want to use another I have to enable multidex – Brandon Zamudio Dec 21 '15 at 19:36
  • I answered bellow, but i think you will get a little headache by using this conventional methods for Networking. I suggest you to check about Retrofit, OkHttp to manage connections, hope it helps. And think about minimize your dependences because multidex, because those libs I mentioned are quite simple and have lower sizes. – diogojme Dec 21 '15 at 20:14

1 Answers1

0

Yes is possible to pass data from a form as url params using HttpUrlConnection. You 've almost got it

In a GET request, the parameters are sent as part of the URL.

In a POST request, the parameters are sent as a body of the request, after the headers.

the way to do it:

 public void sendForm(String formParam1, String formParam2, String formParam3){
    String urlParameters  = "param1="+formParam1+"&param2="+formParam2+"&param3="+formParam3;

    try {
        byte[] postData       = urlParameters.getBytes( StandardCharsets.UTF_8 );
        int    postDataLength = postData.length;
        String request        = "http://sicenet.com.mx/ws/index.php/inicio/editar_clave";
        URL    url            = new URL( request );
        HttpURLConnection conn= (HttpURLConnection) url.openConnection();
        conn.setDoOutput( true );
        conn.setInstanceFollowRedirects( false );
        conn.setRequestMethod( "POST" );
        conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty( "charset", "utf-8");
        conn.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
        conn.setUseCaches( false );

        try(DataOutputStream wr = new DataOutputStream( conn.getOutputStream())) {
            wr.write( postData );
        }

    }catch (Exception e){
        e.printStackTrace();
    }
}

Not tested if it will work.

found in this StackOverFlow answer

Community
  • 1
  • 1
diogojme
  • 2,309
  • 1
  • 19
  • 25
  • @BrandonZamudio You can or you can't? If you can't, what problem you are getting? Update your post and show the php code you are using to receiving the data. – diogojme Dec 22 '15 at 19:16