Daniel Hamutel
Daniel Hamutel

Reputation: 653

How do i POST in java to a server?

I want to send for example a string "hello world" through a stream to a server.

So far i did:

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 do i put the ip and port of the server i want to send the data to ?

Where do i put the string i want to send for example "hello world" ?

And i'm getting errors on this method:

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

On the url: Cannot resolve symbol url

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

On the urlConnection.getOutputStream() unhandled exception.

On

writeStream(out);

Cannot resolve method writeStream

Same for readStream

And in the end on finally: finally without try

I took the example from the offical developers android com:

Developers android

Upvotes: 4

Views: 8624

Answers (5)

gmetax
gmetax

Reputation: 3973

You are totally new as I can see and that android link has some errors in the example. For every "network" work you task that you do, you have to do it asynchronously for android, also you have to add network permission to your manifest file.

For example:

Note : writeStream and readStream is your custom functions for the work you want them 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();
    } 
} 

writeStream function example

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

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

Upvotes: 4

Gustavo
Gustavo

Reputation: 11

   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

Upvotes: 0

Derek K
Derek K

Reputation: 3157

  1. url is the variable of type URL
  2. readStream and writeStream methods you have to create to read/write streams.

You should read about streams in Java and remember you must put network operations in separate thread in Android.

Upvotes: 0

Florian Schaetz
Florian Schaetz

Reputation: 10652

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

"url" should be an URL object, which contains the address you want to connect to (same thing that you enter in your browser), see here.

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

ReadStream will read the response from the server. Unfortunately, reading and witing data is nothing that can be explained easily in such a posting, but there are enough tutorials out there to help you.

Upvotes: 0

AnkeyNigam
AnkeyNigam

Reputation: 2820

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();

Upvotes: 3

Related Questions