在onClick里 请求网络的地方
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
//your code
//网络请求
}).start();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
//your code
//网络请求
}).start();
我的oncreate里也没有网络请求
我的oncreate里也没有网络请求DataService.sendDataByPost 大哥。。你类似这个的不是网络请求是什么?你自己的代码都搞不清楚?
如楼上,你还是先去了解下网络。、线程的理论知识吧。
private EditText mEtName;
private EditText mEtPassword;
private EditText mEtFilePath;
private Button mBtLogin;
private Button mBtLoginPost;
private Button mBtLoginClientGet;
private Button mBtLoginClientPost;
private Button mBtLoginClientPostFile;
private Thread myThread;
private static final int MSG_SUCCESS = 0;//访问成功的标识
private static final int MSG_FAILURE = 1;//访问失败的标识
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mEtName = (EditText) this.findViewById(R.id.et_name);
mEtPassword = (EditText) this.findViewById(R.id.et_password);
mBtLogin = (Button) this.findViewById(R.id.bt_login);
mBtLoginPost = (Button)this.findViewById(R.id.bt_login_post);
mBtLoginClientGet = (Button)this.findViewById(R.id.bt_login_client_get);
mBtLoginClientPostFile = (Button)this.findViewById(R.id.bt_login_client_post_file);
mBtLoginClientPost = (Button)this.findViewById(R.id.bt_login_client_post);
mEtFilePath = (EditText)this.findViewById(R.id.et_file_path);
mBtLogin.setOnClickListener(this);
mBtLoginPost.setOnClickListener(this);
mBtLoginClientGet.setOnClickListener(this);
mBtLoginClientPost.setOnClickListener(this);
mBtLoginClientPostFile.setOnClickListener(this);
new Thread(new Runnable()
{
@Override
public void run()
{
String name = mEtName.getText().toString().trim();
String password = mEtPassword.getText().toString().trim();
if("".equals(name)||"".equals(password)){
Toast.makeText(DemoActivity.this, "用户名或密码不能为空", 0).show();
return;
}
String path = getResources().getString(R.string.servleturl);
try {
} catch (Exception e) {
mHandler.obtainMessage(MSG_FAILURE).sendToTarget();//获取图片失败
return;
}
mHandler.obtainMessage(MSG_SUCCESS).sendToTarget();//获取图片成功,向ui线程发
}
}).start();
}
@Override
public void onClick(View v) {
/*String name = mEtName.getText().toString().trim();
String password = mEtPassword.getText().toString().trim();
if("".equals(name)||"".equals(password)){
Toast.makeText(this, "用户名或密码不能为空", 0).show();
return;
}
String path = getResources().getString(R.string.servleturl);
switch (v.getId()) {
case R.id.bt_login: // 通过get请求 发送数据到服务器
try {
String result = DataService.sendDataByGet(path, name, password);
Toast.makeText(this, result, 0).show();
} catch (Exception e) {
Toast.makeText(this, "访问网路异常", 0).show();
e.printStackTrace();
}
break;
case R.id.bt_login_post:
try {
String result = DataService.sendDataByPost(path, name, password);
Toast.makeText(this, result, 0).show();
} catch (Exception e) {
Toast.makeText(this, "访问网路异常", 0).show();
e.printStackTrace();
}
break;
case R.id.bt_login_client_get:
try {
String result = DataService.sendDataByHttpClientGet(path, name, password);
Toast.makeText(this, result, 0).show();
} catch (Exception e) {
Toast.makeText(this, "访问网路异常", 0).show();
e.printStackTrace();
}
break;
case R.id.bt_login_client_post:
try {
String result = DataService.sendDataByHttpClientPost(path, name, password);
Toast.makeText(this, result, 0).show();
} catch (Exception e) {
Toast.makeText(this, "访问网路异常", 0).show();
e.printStackTrace();
}
break;
case R.id.bt_login_client_post_file:
try {
String filepath = mEtFilePath.getText().toString().trim();
if("".equals(filepath)){
Toast.makeText(this, "路径不能为空", 0).show();
return ;
}
String result = DataService.sendDataByHttpClientPost(path, name, password, filepath);
Toast.makeText(this, result, 0).show();
System.out.println(new String( result.getBytes("iso8859-1"),"utf-8"));
} catch (Exception e) {
Toast.makeText(this, "访问网路异常", 0).show();
e.printStackTrace();
}
break;
}*/
}
private Handler mHandler = new Handler() {
public void handleMessage (Message msg) {//此方法在ui线程运行
String name = mEtName.getText().toString().trim();
String password = mEtPassword.getText().toString().trim();
if("".equals(name)||"".equals(password)){
Toast.makeText(DemoActivity.this, "用户名或密码不能为空", 0).show();
return;
}
String path = getResources().getString(R.string.servleturl);
switch(msg.what) {
case R.id.bt_login: // 通过get请求 发送数据到服务器
try {
String result = DataService.sendDataByGet(path, name, password);
Toast.makeText(DemoActivity.this, result, 0).show();
} catch (Exception e) {
Toast.makeText(DemoActivity.this, "访问网路异常", 0).show();
e.printStackTrace();
}
break;
case R.id.bt_login_post:
try {
String result = DataService.sendDataByGet(path, name, password);
Toast.makeText(DemoActivity.this, result, 0).show();
} catch (Exception e) {
Toast.makeText(DemoActivity.this, "访问网路异常", 0).show();
e.printStackTrace();
}
break;
case R.id.bt_login_client_get:
try {
String result = DataService.sendDataByHttpClientGet(path, name, password);
Toast.makeText(DemoActivity.this, result, 0).show();
} catch (Exception e) {
Toast.makeText(DemoActivity.this, "访问网路异常", 0).show();
e.printStackTrace();
}
break;
case R.id.bt_login_client_post:
try {
String result = DataService.sendDataByHttpClientPost(path, name, password);
Toast.makeText(DemoActivity.this, result, 0).show();
} catch (Exception e) {
Toast.makeText(DemoActivity.this, "访问网路异常", 0).show();
e.printStackTrace();
}
break;
case R.id.bt_login_client_post_file:
try {
String filepath = mEtFilePath.getText().toString().trim();
if("".equals(filepath)){
Toast.makeText(DemoActivity.this, "路径不能为空", 0).show();
return ;
}
String result = DataService.sendDataByHttpClientPost(path, name, password, filepath);
Toast.makeText(DemoActivity.this, result, 0).show();
System.out.println(new String( result.getBytes("iso8859-1"),"utf-8"));
} catch (Exception e) {
Toast.makeText(DemoActivity.this, "访问网路异常", 0).show();
e.printStackTrace();
}
break;
}
}
};
}
这样对不
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
//your code
//网络请求
try {
String result = DataService.sendDataByPost(path, name, password);
//Toast.makeText(this, result, 0).show();
} catch (Exception e) {
//Toast.makeText(this, "访问网路异常", 0).show();
e.printStackTrace();
}
}).start();
break;你没写对。把toast注释掉。