Different URL Stream Behavior

Put your problem here if it does not fit any of the other categories.

Different URL Stream Behavior

Postby paimon.soror » Tue Aug 28, 2012 1:31 am

Hey all, first post here, sorry if in the wrong section. I have a small snippet of code that I have tested outside of the android development environment in a basic Java application

Code: Select all
public static void main(String[] args) {
        try {
            // Establish a URL and open a connection to it. Set it to output mode.
            URLObj = new URL("http://www.myurl.com/login.php");
            connect = URLObj.openConnection();
            connect.setDoOutput(true);   
        }
        catch (MalformedURLException ex) {
            System.out.println("The URL specified was unable to be parsed or uses an invalid protocol. Please try again.");
            System.exit(1);
        }
        catch (Exception ex) {
            System.out.println("An exception occurred. " + ex.getMessage());
            System.exit(1);
        }
      
      
        try {
            // Create a buffered writer to the URLConnection's output stream and write our forms parameters.
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connect.getOutputStream()));
            writer.write("username=paimon.soror&pass=mycrazypass&submit=Login");
            writer.close();
         
            // Now establish a buffered reader to read the URLConnection's input stream.
            BufferedReader reader = new BufferedReader(new InputStreamReader(connect.getInputStream()));
         
            String lineRead = "";
            int sindex, eindex;
         
            // Read all available lines of data from the URL and print them to screen.
            while ((lineRead = reader.readLine()) != null) {
               System.out.println(lineRead);
            }
         
            reader.close();
        }
        catch (Exception ex) {
            System.out.println("There was an error reading or writing to the URL: " + ex.getMessage());
        }


This code works perfectly fine and I get the websites output dumped to the screen. I seem to be having an issue however with the android emulator and I am not too sure if it will propogate to the device. What happens is that the code seems to flow properly, except that the following line doesn't enter the loop since readLine returns a null:

Code: Select all
// Read all available lines of data from the URL and print them to screen.
            while ((lineRead = reader.readLine()) != null) {
               System.out.println(lineRead);
            }


The only permission that I have added to the project is

Code: Select all
<uses-permission android:name="android.permission.INTERNET"/>
paimon.soror
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Aug 28, 2012 1:24 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 13 guests