【发布时间】:2013-06-23 05:33:50
【问题描述】:
public class TestProjMain extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText eText = (EditText) findViewById(R.id.address);
final TextView tView = (TextView) findViewById(R.id.pagetext);
final Button button = (Button) findViewById(R.id.ButtonGo);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
try {
tView.setText("");
// Perform action on click
URL url = new URL(/*"http://www.google.com"*/eText.getText().toString());
URLConnection conn = url.openConnection();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = "";
while ((line = rd.readLine()) != null) {
tView.append(line);
}
}
catch (Exception e) {
}
}
});
}
}
我写了这个,但它不起作用。我也使用了 httpclient 代码,但这也不起作用。每当我执行此功能时,我的模拟器都会停止响应。不知道问题出在哪里??有人请帮忙!在此先感谢...
【问题讨论】:
-
您不能在 UI 线程上执行此操作。创建一个新线程或服务,获取代码并解析它,然后使用处理程序告诉您的 UI 线程您已完成并执行某些操作。
-
他可以更好地使用AsynTask类
-
这就是它没有响应的原因:stackoverflow.com/questions/5513457/…
-
你是否在清单中添加了互联网权限?
标签: android