============问题描述============
private static final String URL = " ";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) this.findViewById(R.id.textView2);
user = (EditText) this.findViewById(R.id.editText1);
password = (EditText) this.findViewById(R.id.editText2);
button = (Button) this.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 实现连接服务器的功能
init();
}
// 用POST的方法实现
private void init() {
// TODO Auto-generated method stub
boolean flag = false; // 成功与否的标记
String user1 = user.getText().toString().trim();
String password1 = password.getText().toString().trim();
try {
HttpPost request = new HttpPost(URL);
List<NameValuePair> parms = new ArrayList<NameValuePair>();
parms.add(new BasicNameValuePair("id", user));
parms.add(new BasicNameValuePair("password",password1));
request.setEntity(new UrlEncodedFormEntity(parms,
HTTP.UTF_8));
HttpResponse response = new DefaultHttpClient()
.execute(request);
if (response.getStatusLine().getStatusCode() != 404) { // 现在已经发现了数据了
flag = Boolean.parseBoolean(EntityUtils.toString(response.getEntity()).trim());
response.getStatusLine().getStatusCode());
}
} catch (Exception e) {
// TODO: handle exception
tv.setText("WEB服务器连接失败……");
}
if (flag) {
tv.setText("用户登录成功!" + flag);
} else {
tv.setText("用户登录失败!" + flag);
}
}
});
}
============解决方案1============
你的url呢?没给具体ip路径? 模拟器本来就在电脑上,可以直接访问服务器。
你在真机上需要关闭电脑防火墙,通过具体的内网ip地址访问。真机和服务端要处于同一局域网