How do I make a POST in java to the server?

I want to send, for example, the string "hello world" via stream to the server.

So far I have done:

private void PostData() {

        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        try {
            urlConnection.setDoOutput(true);
            urlConnection.setChunkedStreamingMode(0);

            OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
            writeStream(out);

            InputStream in = new BufferedInputStream(urlConnection.getInputStream());
            readStream(in);
            finally {
                urlConnection.disconnect();
            }
        }
    }

      

But not sure where I can place the ip and port of the server to which I want to send data?

Where can I put the string I want to send, for example "hello world"?

And I am getting errors on this method:

HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

      

On URL: Cannot resolve character url

OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());

      

In an unhandled exception urlConnection.getOutputStream ().

On

writeStream(out);

      

Cannot resolve method writeStream

Same for readStream

And in the end: finally, without trying

I took an example from the official android com developers:

Android developers

+3


source to share


5 answers


You can do something like: -



        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "text/plain");

        String input = YOUR_INPUT_STRING;

        OutputStream os = conn.getOutputStream();
        os.write(input.getBytes());
        os.flush();

      

+2


source


You are brand new as I see and this android link has some bugs in this example. For every "networking" work you do, you must do it asynchronously for android, and you also need to add network permissions to your manifest file.

For example:

Note. writeStream

and readStream

- your custom functions for the job you want to do.




private void PostData() { 
    String IPPORT = "www.android.com"; // or example 192.168.1.5:80
    URL url = new URL("http://"+IPPORT+"/");
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    try { 
        urlConnection.setDoOutput(true);
        urlConnection.setChunkedStreamingMode(0);

        OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
        writeStream(out);

        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        readStream(in);

    } 
    finally { 
        urlConnection.disconnect();
    } 
} 

      

Example writeStream function

private void writeStream(OutputStream out){
    String output = "Hello world";

    out.write(output.getBytes());
    out.flush();
}

      

+2


source


Well, I'm afraid you'll have to define the "url" variable and the "writeStream" and "readStream" methods.

"url" must be a url object that contains the address you want to connect to (the same as you type into your browser), see here .

WriteStream is where you write your data to the server where you write "Hello World".

ReadStream will read the response from the server. Unfortunately, reading and writing data is something that can be easily explained in a post like this, but there are enough tutorials to help you.

0


source


  • url is a variable of type url
  • the readStream and writeStream methods that you need to create to read / write streams.

You should read up on threads in Java and remember that you have to put network operations on a separate thread on Android.

0


source


   I had the same problem but with this code, I can solve

    String userAgent = "Mozilla/5.0 (X11; Linux x86_64; rv:26.0) Gecko/20100101 Firefox/26.0";
    String address = "http://localhost/form.php";
    String forSending = "cadena";
    String charset = "UTF-8";

    String stringToSend = URLEncoder.encode(forSending, charset);

    URL URL = new URL(address);
    URLConnection connection = URL.openConnection();
    connection.addRequestProperty("User-Agent", userAgent);

    connection.setDoOutput(true);
    OutputStreamWriter out = new OutputStreamWriter(
            connection.getOutputStream());
    out.write("nombre=" + stringToSend); 
    out.close();

    BufferedReader in = new BufferedReader(
            new InputStreamReader(
                    connection.getInputStream()));
    String response;
    while((response = in.readLine()) != null)
        System.out.println(response);
    in.close();

      

help link: http://aljavier.github.io/manipulando-urls-y-haciendo-solicitudes-http-con-java.html Derechos de autor

-1


source







All Articles