我正在尝试登录一个网站并使用 android 模拟器显示它的源代码,但我无法使其工作,但仅在基于 java 的情况下,它正在工作。
这是我的代码:
package auth.test;
import android.app.Activity;
import android.os.Bundle;
// used for interacting with user interface
import android.widget.TextView;
import android.widget.EditText;
// used for passing data
// used for connectivity
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class AuthActivity extends Activity {
/** Called when the activity is first created. */
//Handler h;
private static URL URLObj;
private static URLConnection connect;
EditText eText;
TextView tView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tView=(TextView)findViewById(R.id.textView1);
initControls();
}
public void initControls()
{
try {
URLObj = new URL("http://students.usls.edu.ph");
connect = URLObj.openConnection();
connect.setDoOutput(true);
}
catch (MalformedURLException ex) {
tView.setText("The URL specified was unable to be parsed or uses an invalid protocol. Please try again.");
//System.exit(1);
}
catch (Exception ex) {
tView.setText("An exception occurred. " + ex.getMessage());
//System.exit(1);
}
try {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connect.getOutputStream()));
writer.write("username=0920204&password=******");
//writer.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(connect.getInputStream()));
String lineRead="";
while((lineRead=reader.readLine())!=null)
{
tView.append(lineRead + "\n");
}
reader.close();
}
catch (Exception ex) {
tView.setText("There was an error reading or writing to the URL: " + ex.getMessage());
}
}
}
所以,不是显示主页的源代码,而是显示登录页面的源代码。所以我认为代码的 Bufferedwriter 部分有问题。