多寫技術文檔,多寫技術文檔,多寫技術文檔,重要的事情說三遍......html
-------------------------------------------------------------------------------------------------------------------java
功能需求:Android客戶端解析和顯示雲服務器返回的xml文件。android
思路:涉及到Android端應用和Java ee端web應用的編碼實現。web
示例:Android客戶端獲取騰訊雲服務器上的視頻資訊數據庫
步驟:apache
一、開發環境部署編程
二、web應用部分開發windows
a、在Java ee下(安裝在本地筆記本端)新建「Dynamic Web Project」;瀏覽器
b、而後新建一個servlet,同時注意web.xml中的相關配置狀況,截圖以下:tomcat
(注:servlet是放在HTTP Web服務器上面運行的Java程序。當用戶經過客戶端和服務器交互時,請求(request)會送給web服務器。若是請求須要Java的servlet時,服務器會執行或調用已經執行的servlet程序代碼。servlet只是在服務器上運行的程序代碼,執行用戶發出請求所要的結果。)
c、其中servlet核心代碼以下:
public class ListServelet extends HttpServlet { private static final long serialVersionUID = 1L; private VideoNewsService service=new VideoNewsServiceBean(); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<News> videos = service.getLastNews(); request.setAttribute("videos", videos); request.getRequestDispatcher("/WEB-INF/page/videonews.jsp").forward(request, response); } }
新建一個javabean,代碼以下:資訊id+標題+時長
public class VideoNewsServiceBean implements VideoNewsService { public List<News> getLastNews(){ List<News> newes = new ArrayList<News>(); newes.add(new News(90, "喜羊羊與灰太狼全集", 78)); newes.add(new News(10, "實拍艦載直升東海救援演習", 28)); newes.add(new News(56, "喀麥隆VS荷蘭", 70)); return newes; } }
在業務bean中定義一個方法返回一個集合,例如:News,集合存放了數據庫最新的視頻資訊,集合裏的元素類型採用一個javabean描述每一條視頻資訊。
public class News { private Integer id; private String title; private Integer timelength; public News(Integer id, String title, Integer timelength) { this.id = id; this.title = title; this.timelength = timelength; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public Integer getTimelength() { return timelength; } public void setTimelength(Integer timelength) { this.timelength = timelength; } }
此外,Java ee是面向接口編程,將業務bean中的方法進行接口抽取,同時繼承實現這個接口中的方法。接口代碼以下:
public interface VideoNewsService { /** * 獲取最新的視頻資訊 * @return */ public List<News> getLastNews(); }
e、 另外在表現層構造xml數據,在WebContent-WEB-INF下新建一個「page」文件夾,而後再新建一個jsp文件,名稱「videonews」。contentType=「text/xml」 ,代碼以下:
1 <%@ page language="java" contentType="text/xml; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><?xml version="1.0" encoding="UTF-8"?> 2 <videonews><c:forEach items="${videos}" var="video"> 3 <news> 4 <id>${video.id}</id> 5 <title>${video.title}</title> 6 <timelength>${video.timelength}</timelength> 7 </news></c:forEach> 8 </videonews>
其中xml數據的呈現形式能夠在jsp文件中進行定義,同時須要在lib文件下添加第三方類庫jstl,以下:
f、在servlet中須要引入新建的jsp文件,這樣才能自定義xml格式文件,以下:
其中集合中的視頻資訊對象傳遞給jsp文件,把對象放在request範圍給定key,接着把請求內部轉發給jsp。指定jsp文件路徑。
g、在javaee下把應用部署到tomcat服務器以後,在瀏覽器中輸入路徑:http://localhost:8080/videonews/ListServlet出現xml數據格式,證實web應用沒問題了,即從服務器端返回了xml格式的數據而且顯示在屏幕上。
h、將web項目部署到騰訊雲服務器上
第一步,在java ee中將web項目導出,導出爲war格式;
第二步,將第一步導出的war文件放入騰訊雲服務器Tomcat的webAPP目錄下,而後從bin---startup.bat啓動Tomcat,先在服務器試試http://localhost:8080/項目名稱/ListServelet可否成功運行,能夠的話,不要關閉服務器的Tomcat,回到本身筆記本,在瀏覽器輸入http://服務器公網IP:8080/項目名稱/ListServelet便可訪問雲服務器web項目.
三、Android部分開發
a、引入OkHttp來發送HTTP請求方式,在build.gradle添加OkHttp依賴文件,以下:
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.0.1' testCompile 'junit:junit:4.12' compile 'com.squareup.okhttp3:okhttp:3.4.1' }
b、此外,android部分的核心是對xml數據進行解析,代碼以下:
1 class VideoNewsService { 2 /** 3 * 獲取視頻資訊 4 * 5 * @return 6 * @throws Exception 7 */ 8 static List<News> newses = new ArrayList<>(); 9 static List<News> sendRequestWithOkHttp() { 10 new Thread(new Runnable() { 11 @Override 12 public void run() { 13 try { 14 OkHttpClient client = new OkHttpClient(); 15 Request request = new Request.Builder() 16 // 指定訪問的服務器地址是電腦本機 17 .url("http://雲服務器公網IP:8080/VideoNews/ListServelet") 18 .build(); 19 Response response = client.newCall(request).execute(); 20 String responseData = response.body().string(); 21 parseXML(responseData); 22 23 } catch (Exception e) { 24 e.printStackTrace(); 25 } 26 } 27 }).start(); 28 return newses; 29 } 30 31 private static List<News> parseXML(String xmlData) throws Exception { 32 News news = null; 33 XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 34 XmlPullParser parser = factory.newPullParser(); 35 parser.setInput(new StringReader(xmlData)); 36 int event = parser.getEventType(); 37 while (event != XmlPullParser.END_DOCUMENT) { 38 switch (event) { 39 case XmlPullParser.START_TAG:
40
41 if ("id".equals(parser.getName())) { 42 int id = Integer.valueOf(parser.nextText()); 43 news = new News(); 44 news.setId(id); 45 } else if ("title".equals(parser.getName())) { 46 news.setTitle(parser.nextText()); 47 } else if ("timelength".equals(parser.getName())) { 48 news.setTimelength(Integer.valueOf(parser.nextText())); 49 } 50 break; 51 52 case XmlPullParser.END_TAG: { 53 if ("news".equals(parser.getName())) { 54 newses.add(news); 55 news = null; 56 } 57 break; 58 } 59 default: 60 break; 61 } 62 event = parser.next(); 63 } 64 return newses; 65 } 66 }
News.java代碼以下:
1 public class News { 2 private Integer id; 3 private String title; 4 private Integer timelength; 5 public News(){} 6 public News(Integer id, String title, Integer timelength) { 7 this.id = id; 8 this.title = title; 9 this.timelength = timelength; 10 } 11 public Integer getId() { 12 return id; 13 } 14 public void setId(Integer id) { 15 this.id = id; 16 } 17 public String getTitle() { 18 return title; 19 } 20 public void setTitle(String title) { 21 this.title = title; 22 } 23 public Integer getTimelength() { 24 return timelength; 25 } 26 public void setTimelength(Integer timelength) { 27 this.timelength = timelength; 28 } 29 30 }
MainActivty.class的核心代碼以下:
1 public class MainActivity extends AppCompatActivity { 2 /** Called when the activity is first created. */ 3 @Override 4 public void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_main); 7 8 ListView listView = (ListView) this.findViewById(R.id.listView); 9 10 try { 11 List<News> videos = VideoNewsService.sendRequestWithOkHttp();//需修改爲你本機的Http請求路徑 12 List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>(); 13 for(News news : videos){ 14 HashMap<String, Object> item = new HashMap<String, Object>(); 15 item.put("id", news.getId()); 16 item.put("title", news.getTitle()); 17 item.put("timelength", getResources().getString(R.string.timelength) 18 + news.getTimelength()+ getResources().getString(R.string.min)); 19 data.add(item); 20 } 21 SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item, 22 new String[]{"title", "timelength"}, new int[]{R.id.title, R.id.timelength}); 23 listView.setAdapter(adapter); 24 } catch (Exception e) { 25 e.printStackTrace(); 26 } 27 } 28 }
c、最終運行成功的效果圖,以下:
--------------------------------------------------------------------------------------------------------------------
參考資料:
http://www.cnblogs.com/diyunfei/p/6826557.html //怎樣將本地web項目部署到騰訊雲服務器上?
https://jingyan.baidu.com/article/3a2f7c2e62d25e26afd611fa.html //WIN7系統JavaEE(java+tomcat7+Eclipse)環境配置