记在两周Android实训之后
正月初十回校,十一開(kāi)始實(shí)訓(xùn)課程。要求是在兩周之后交出一個(gè)Android APP。
對(duì)于Android,我的印象就是跟java一樣,但是沒(méi)搞過(guò)。
第一周,java,從數(shù)據(jù)類型到講到servlet,中間講了集合、線程、IO、單例模式,總之是撈干的來(lái)。
第一周結(jié)束,也算是收獲頗豐,撿起了扔下兩年的代碼,因?yàn)槭菍W(xué)習(xí)就放棄了使用框架的想法,自己試著用反射機(jī)制簡(jiǎn)單的封裝了servlet。長(zhǎng)時(shí)間不寫代碼的感受是,對(duì)于持有其他對(duì)象引用的這個(gè)事,經(jīng)常會(huì)忘記初始化就拿來(lái)用了,報(bào)錯(cuò)基本都是空指針異常。
第二周,Android。
其實(shí)怎么看都跟WEB類似,Android的前端跟WEB的前端并沒(méi)有太多區(qū)別。給組件一個(gè)id,在相應(yīng)的activity中獲取組件,對(duì)組件進(jìn)行監(jiān)聽(tīng),獲取內(nèi)容。其實(shí)Android的組件都很成熟了,那些看起來(lái)炫酷的東西,Android本身都已經(jīng)封裝好了,拿來(lái)用就是了。
Android前端設(shè)計(jì)的時(shí)候跟WEB不同的是,受限于屏幕大小,我們需要把更多的事件放到同一個(gè)組件中。比如說(shuō)一個(gè)搜索框,在WEB中我們可以用一個(gè)下拉框來(lái)控制搜索內(nèi)容的類別,但在WEB中這并不適用,如果你在應(yīng)用中見(jiàn)到搜索框加下拉選擇類型,這種軟件還是放棄吧,辣眼睛。合適的做法是你可以在搜索框下加入幾個(gè)大方美觀的標(biāo)簽,搜索的同時(shí)獲取標(biāo)簽內(nèi)容。另一個(gè)就是你在響應(yīng)搜索事件后,搜索多張表,浪費(fèi)點(diǎn)時(shí)間,但卻簡(jiǎn)化了用戶的操作。
另一個(gè)遇到的問(wèn)題是Android studio 對(duì)于前端頁(yè)面報(bào)錯(cuò)不明顯。如果你的布局里有多個(gè)組件,而你又不小心忘了寫oritentation,很不幸有些東西會(huì)顯示不出來(lái)。這個(gè)問(wèn)題讓我誤以為我的Intent沒(méi)有把參數(shù)傳過(guò)來(lái),折騰了好久(哭)。
對(duì)于APP的網(wǎng)絡(luò)連接,我們使的是老控件。
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 {//通過(guò)地址創(chuàng)建URL對(duì)象URL url = new URL(urlStr);//建立連接URLConnection conn = url.openConnection();//接收返回的數(shù)據(jù)流InputStream is = conn.getInputStream();//讀取返回的內(nèi)容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());} }這里犯得另一個(gè)錯(cuò)誤是在后臺(tái)返回?cái)?shù)據(jù)時(shí)寫成out.println();
????????多了一個(gè)換行符,對(duì)返回結(jié)果進(jìn)行字符串匹配時(shí),一直不對(duì),我差點(diǎn)就懷疑人生了。
結(jié)尾附上另外兩個(gè)封裝代碼(數(shù)據(jù)庫(kù)和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;//μ¥áD?£ê?private static final DBUtil dbutil = new DBUtil();private DBUtil(){}public static DBUtil getInstace(){return dbutil;}//??è?êy?Y?aá??ó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;}//2é?ˉ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;}//DT???üD?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();}}//1?±?á??ó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();}} }
總結(jié)
以上是生活随笔為你收集整理的记在两周Android实训之后的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 祝贺JeecgBoot获评为2019年度
- 下一篇: 亿级流量 | 蚂蚁金服分布式事务实践解析