Hello, the following code uses POST method to communicate with the Web Server, and the response is displayed in the console (not parsed in a Servlet, etc). When compiled as a java application, the program works as expected returning the response as html text.
My aim is to have an Android app that will communicate with servers using various protocols- and I am using this as an example in learning how that technique can be done. Therefore I would wish Android to get the response, and displaying it in 'TextView?' or somewhere. I thought this was going to be a straight-forward thing, but I have been stuck at this point for days now. Can somebody assist if possible. Thanks. The code follows package Com; import java.net.*; import java.io.*; public class FormPoster { private URL url; private QueryString query = new QueryString(); public FormPoster (URL url){ if (!url.getProtocol().toLowerCase().startsWith("http")){ throw new IllegalArgumentException("Posting only works for http URLs"); } this.url = url; } public void add(String name, String value){ query.add(name, value); } public URL getURL(){ return this.url; } public InputStream post() throws IOException { URLConnection uc = url.openConnection(); uc.setDoOutput(true); OutputStreamWriter out = new OutputStreamWriter(uc.getOutputStream (), "ASCII"); out.write(query.toString()); out.write("\r\n"); out.flush(); out.close(); return uc.getInputStream(); } public static void main(String args[]){ URL url; if (args.length > 0){ try{ url = new URL(args[0]); } catch (MalformedURLException ex){ System.err.println("Usage: java FormPoster url"); return; } } else { try{ url = new URL("http://www.cnn.com"); } catch (IOException ex){ System.err.println(ex); return; } } FormPoster poster = new FormPoster(url); poster.add("name","Charles"); poster.add("email", "ma...@yahoo.com"); try{ InputStream in = poster.post(); InputStreamReader r = new InputStreamReader(in); int c; while((c = r.read())!= -1){ System.out.print((char) c); } System.out.println(); in.close(); } catch (IOException ex){ System.err.println(ex); } } } --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Beginners" group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~----------~----~----~----~------~----~------~--~---