本片利用局域网中另一台电脑启动Tomcat Server,然后用Android手机采取Get和Post两种方式进行连接
首先,任何涉及网络的操作都要获取网络操作权限,在AndroidManifest.xml中添加如下权限
<uses-permission android:name="android.permission.INTERNET" />
Get方式的一般流程是:
写好带参数的url ---> 地址重写 ---> 获取该url的HttpURLConnection ---> 准备字节数组 ---> 获取HttpURLConnection的输入流并读入字节数组中 ---> 其他组数据操作
public class MainActivity extends Activity { private EditText idText = null; private EditText pwText = null; private TextView infoText = null; private Button connectBtn = null; private boolean flag = false; @SuppressLint("NewApi") @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /*下面两段代码使Android3.0以上系统可以让http代码使用主UI线程,因为3.0以上系统对UI资源的使用更严格*/ StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectDiskReads().detectDiskWrites().detectNetwork() .penaltyLog().build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() .detectLeakedSqlLiteObjects().penaltyLog() .penaltyDeath().build()); idText = (EditText) findViewById(R.id.idText); pwText = (EditText) findViewById(R.id.pwText); infoText = (TextView) findViewById(R.id.infoText); connectBtn = (Button) findViewById(R.id.connectBtn); connectBtn.setOnClickListener(new ConnectBtnListener()); // http://172.16.1.201:8080/androidweb/ // Get请求,地址重写方式 try { URL url = new URL("http", "172.16.1.210", 8080, "/androidweb/android.jsp?id=shuai&password=shuai"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); byte data[] = new byte[512]; int len = conn.getInputStream().read(data); // 输入流读取 if (len > 0) { // 已经读取到内容 String temp = new String(data, 0, len).trim(); flag = Boolean.parseBoolean(temp) ; // 取出里面的boolean型数据 } conn.getInputStream().close() ; } catch (MalformedURLException e) { this.infoText.setText("服务器连接失败!"); } catch (IOException e) { this.infoText.setText("IO错误!"); } if (flag) { this.infoText.setText("用户连接成功!"); } else { this.infoText.setText("用户连接失败!"); } }
Post方式的一般流程是:
实例化HttpPost请求,参数为url ---> 准备参数列表 ---> 向列表中添加参数 ---> 设置post请求实体,即为向请求中添加参数和编码 ---> 执行请求,获得服务器响应 ---> 判断响应头文件的状态码 ---> 获取响应实体对象,提取信息
private class ConnectBtnListener implements OnClickListener { @Override public void onClick(View v) { //Post请求 HttpPost postRequest = new HttpPost(urlString); //准备参数列表,泛型为基本键值对 List<NameValuePair> params = new ArrayList<NameValuePair>(); // 添加参数 params.add(new BasicNameValuePair("id", MainActivity.this.idText.getText().toString())); params.add(new BasicNameValuePair("password", MainActivity.this.pwText.getText().toString())); try { // 设置post请求实体,即为向请求中添加参数和编码 postRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); // 执行请求,获得服务器响应 HttpResponse response = new DefaultHttpClient().execute(postRequest); // 判断响应头文件的状态码 if (response.getStatusLine().getStatusCode() == 200) { // 获取响应实体对象,提取信息 MainActivity.this.flag = Boolean.parseBoolean(EntityUtils.toString(response.getEntity()).trim()); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if (MainActivity.this.flag) { MainActivity.this.infoText.setText("用户连接成功!"); } else { MainActivity.this.infoText.setText("用户连接失败!"); } } } }
下面是服务器端jsp文件:
<% String id = request.getParameter("id") ; String password = request.getParameter("password") ; %> <% if ("shuai".equals(id) && "shuai".equals(password)) { %> true <% } else { %> false <% } %>