Hi Plus Minus,
I saw your sample code.
I am running Apache Server in my pc.
I created a very small PHP script which will take input.
PHP Script
<?php
echo "POSTed data: '".$_POST['mydata']."'";
?>
Location of the PHP File:
http://localhost/postresponse.phpI wrote few lines of code to Post HTTP Request with String Info:
package http.pack;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import android.app.Activity;
import android.os.Bundle;
public class http_sample extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String POSTText = null;
try {
POSTText = "mydata=" + URLEncoder.encode("HELLO, ANDROID HTTPPostExample - by anddev.org", "UTF-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
doPost("http://localhost/postresponse.php", POSTText);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private InputStream doPost(String urlString, String content) throws IOException {
URL url = new URL(urlString);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
InputStream in = null;
OutputStream out;
byte[] buff;
con.setRequestMethod("POST");
URLConnection.setDefaultRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setDoOutput(true);
con.setDoInput(true);
con.connect();
out = con.getOutputStream();
buff = content.getBytes("UTF8");
out.write(buff);
out.flush();
out.close();
in = con.getInputStream();
return in;
}
}
Now I get exception error when con.connect(); is hit.
Am I suppose include some thing more.
Please guide me to run this code successfully.
Vishal