import java.net.URL;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.content.Intent;
import android.os.Bundle;
public class MenuActivity extends ActionBarActivity {
private TextView textView;
private Button button;
private String username;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
init();
Intent intent = getIntent();
username = intent.getStringExtra("username");
textView.setText("欢迎您," + username);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
new Thread(postData).run();
}
});
}
public void init(){
textView = (TextView) findViewById(R.id.textView1);
button = (Button) findViewById(R.id.button1);
}
Runnable postData = new Runnable(){
public void run() {
String httpUrl = "http://192.168.3.5:8080/showPost/ShowPost?comment=123";
try {
URL url = new URL(httpUrl);
url.openConnection();
} catch (Exception e) {
e.printStackTrace();
}
}
};
}
主要的问题出在new Thread(),和Runnable那里吧?之前版本的android应该是不用new一个线程,但是新版本就得这么写了,可是服务器端还是没有反应额,求大神指教,谢谢~~~
android.os.NetworkOnMainThreadException问题,为什么new了一个thread还是无法连接到服务器端?
最新推荐文章于 2021-07-29 09:23:32 发布