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