记在两周Android实训之后
正月初十回校,十一开始实训课程。要求是在两周之后交出一个Android APP。
对于Android,我的印象就是跟java一样,但是没搞过。
第一周,java,从数据类型到讲到servlet,中间讲了集合、线程、IO、单例模式,总之是捞干的来。
第一周结束,也算是收获颇丰,捡起了扔下两年的代码,因为是学习就放弃了使用框架的想法,自己试着用反射机制简单的封装了servlet。长时间不写代码的感受是,对于持有其他对象引用的这个事,经常会忘记初始化就拿来用了,报错基本都是空指针异常。
第二周,Android。
其实怎么看都跟WEB类似,Android的前端跟WEB的前端并没有太多区别。给组件一个id,在相应的activity中获取组件,对组件进行监听,获取内容。其实Android的组件都很成熟了,那些看起来炫酷的东西,Android本身都已经封装好了,拿来用就是了。
Android前端设计的时候跟WEB不同的是,受限于屏幕大小,我们需要把更多的事件放到同一个组件中。比如说一个搜索框,在WEB中我们可以用一个下拉框来控制搜索内容的类别,但在WEB中这并不适用,如果你在应用中见到搜索框加下拉选择类型,这种软件还是放弃吧,辣眼睛。合适的做法是你可以在搜索框下加入几个大方美观的标签,搜索的同时获取标签内容。另一个就是你在响应搜索事件后,搜索多张表,浪费点时间,但却简化了用户的操作。
另一个遇到的问题是Android studio 对于前端页面报错不明显。如果你的布局里有多个组件,而你又不小心忘了写oritentation,很不幸有些东西会显示不出来。这个问题让我误以为我的Intent没有把参数传过来,折腾了好久(哭)。
对于APP的网络连接,我们使的是老控件。
import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject;import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map;/*** Created by Administrator on 2018/3/9.*/public class NetworkUtil {public static final String BASE_URL = "http://192.168.3.69:8080/networkBack/";public static final String QUERY_PERSON = BASE_URL + "QueryAllServlet";public static final String PAGE_PERSON = BASE_URL + "PageQueryServlet";public static final String INSERT_PERSON = BASE_URL + "InsertPerServlet";public static JSONObject getJSONByURL(String urlStr) throws IOException, JSONException {return new JSONObject(getDataByURL(urlStr));}public static JSONArray getJSONArrByURL(String urlStr) throws IOException, JSONException {return new JSONArray(getDataByURL(urlStr));}public static String getDataByURL(String urlStr) throws IOException {//通过地址创建URL对象URL url = new URL(urlStr);//建立连接URLConnection conn = url.openConnection();//接收返回的数据流InputStream is = conn.getInputStream();//读取返回的内容BufferedReader br = new BufferedReader(new InputStreamReader(is));StringBuffer buffer = new StringBuffer();String line = null;while ((line = br.readLine()) != null) {buffer.append(line);}br.close();return buffer.toString();}public static String postDataByUrl(String urlStr, Map<String, String> map) throws IOException {HttpClient client = new DefaultHttpClient();HttpPost post = new HttpPost(urlStr);List<NameValuePair> list = new ArrayList<>();Iterator<Map.Entry<String, String>> iter = map.entrySet().iterator();while(iter.hasNext()) {Map.Entry<String, String> entry = iter.next();NameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue());list.add(pair);}post.setEntity(new UrlEncodedFormEntity(list));HttpResponse response = client.execute(post);return EntityUtils.toString(response.getEntity());} }这里犯得另一个错误是在后台返回数据时写成out.println();
多了一个换行符,对返回结果进行字符串匹配时,一直不对,我差点就怀疑人生了。
结尾附上另外两个封装代码(数据库和servlet):
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List;public class DBUtil {private String url = "jdbc:mysql://localhost/xjtuse";private String user = "root";private String password = "root";Connection conn = null;PreparedStatement ps = null;ResultSet rs = null;//µ¥ÁÐģʽprivate static final DBUtil dbutil = new DBUtil();private DBUtil(){}public static DBUtil getInstace(){return dbutil;}//»ñÈ¡Êý¾Ý¿âÁ¬½Ópublic Connection getConnection() {try {Class.forName("com.mysql.jdbc.Driver");conn = DriverManager.getConnection(url, user, password);} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return conn;}//²éѯpublic ResultSet query(String sql, List<Object> params){try {ps = conn.prepareStatement(sql);if(params != null && params.size() > 0){for(int i = 0; i < params.size(); i++){ps.setObject(i + 1, params.get(i));}}rs = ps.executeQuery();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return rs;}//Ð޸ĸüÐÂpublic void update(String sql, List<Object> params){try {ps = conn.prepareStatement(sql);if(params != null && params.size() > 0){for (int i = 0; i < params.size(); i++){ps.setObject(i + 1, params.get(i));}}ps.executeUpdate();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}//¹Ø±ÕÁ¬½Ópublic void close(){try {if(rs != null){rs.close();}if(ps != null){ps.close();}if(conn != null){conn.close();}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;public class BasicServlet extends HttpServlet{private static final long serialVersionUID = 1L;@Overrideprotected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType("text/html;charset=utf-8");request.setCharacterEncoding("utf-8");String methodname = request.getParameter("methodname");//»ñÈ¡µ±Ç°ÀàµÄÈ«ÀàÃûClass clazz = this.getClass();try {//·´Éä»ñµÃ·½·¨Method method = clazz.getMethod(methodname, HttpServletRequest.class, HttpServletResponse.class);//·´Éäµ÷ÓÃString path = (String)method.invoke(this, request, response);if(path != null && !path.equals("")){request.getRequestDispatcher(path).forward(request, response);return;}} catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SecurityException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}} }
总结
以上是生活随笔为你收集整理的记在两周Android实训之后的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 祝贺JeecgBoot获评为2019年度
- 下一篇: windows 下Android的开发准