0

This is originally a Java code. I tried to convert it for Android. In my XML I have a textbox. The text.setText is supposed to write the output to the textbox, but nothing is appearing.

    private static URL URLObj;
    private static URLConnection connect;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView text = new TextView(this);

        try {

            URLObj = new URL("http://students.usls.edu.ph");
            connect = URLObj.openConnection();
            connect.setDoOutput(true);  
        }
        catch (MalformedURLException ex) {
            text.setText("The URL specified was unable to be parsed or uses an invalid protocol. Please try again.");
            System.exit(1); 
        }
        catch (Exception ex) {
            text.setText("An exception occurred. " + ex.getMessage());
            System.exit(1);
        }


        try {

            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connect.getOutputStream()));
            writer.write("username=0920204&password=183456&submit=Login");
            writer.close();

            BufferedReader reader = new BufferedReader(new InputStreamReader(connect.getInputStream()));

            String lineRead = "";


            while ((lineRead = reader.readLine()) != null) {
                text.setText(lineRead);
            }

            reader.close();
        }
        catch (Exception ex) {
            text.setText("There was an error reading or writing to the URL: " + ex.getMessage());
        }
    }



}
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by stevelg
1

One problem I can see is that you have created the textView in the Java code rather then within main.xml but not added the View to the layout. You czan create widgets in xml or in code (or both) but they need adding to a view group using
this.addContentView(text);
You may also need to add parameters for the widget size.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.