當前位置:首頁 » 雲伺服器 » app如何獲取伺服器內容的

app如何獲取伺服器內容的

發布時間: 2023-08-08 16:21:35

Ⅰ APP是怎樣獲取和上傳數據到雲端資料庫

一般MySQL都不由APP進行直接的遠程連接,而是交由後台伺服器進行封裝處理的。
舉個例子。APP訪問--》後台PHP伺服器--》PHP訪問本地MySQL資料庫--》PHP返回查詢給APP
所以,一般的APP分為兩部分,APP前端開發,PHP後台開發

Ⅱ android怎麼獲取伺服器數據

一:基於Http協議獲取數據方法。二:基於SAOP協議獲取數據方法,
這篇文章主要是將關於使用Http協議獲取伺服器端數據,這里我們採取的伺服器端技術為java,框架為Struts2,或者可以有Servlet,又或者可直接從JSP頁面中獲取數據。
那麼,接下來我們便開始這一路程:
首先:編寫伺服器端方法,我這里採用的MVC框架是Struts2,目的很單純,就是為了以後做個完整的商業項目,技術配備為:android+SSH。當然,篇幅有限,我這里就直接用Strtus2而已。
伺服器端:新建WebProject ,選擇Java ee 5.0.
為了給項目添加Struts2的支持,我們必須導入Struts2的一些類庫,如下即可(有些jar包是不必的,但是我們後來擴展可能是要使用到的,就先弄進去):
1: xwork-core-2.2.1.1.jar
2: struts2-core-2.2.1.1.jar
3: commons-logging-1.0.4.jar
4: freemarker-2.3.16.jar
5: ognl-3.0.jar
6: javassist-3.7.ga.jar
7:commons-ileupload.jar
8:commons-io.jar
9:json-lib-2.1-jdk15.jar 處理JSON格式數據要使用到
10:struts2-json-plugin-2.2.1.1.jar 基於struts2的json插件
以上的jar包,需要放在WebRoot/WEB-INF/lib目錄下
然後在web.xml文件中敲下:
View Code
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!-- 定義Struts2的核心控制器:FilterDispatcher -->
<filter>
<!-- 定義核心Filter的名稱 -->
<filter-name>struts2</filter-name>
<!-- 定義Filter的實現類 -->
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>
然後編寫struts.xml文件,並放在WebRoot/WEB-INF/lib目錄下:如下代碼:
View Code
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>

<!-- setting encoding,DynamicMethod,language
<constant name="struts.custom.i18n.resources" value="messageResource"></constant>
-->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>

<!-- add package here extends="struts-default"-->
<package name="dongzi" extends="json-default"> <!--需要將struts-default改為json-default-->
<!-- setting action -->
<action name="login" class="com.dongzi.action.loginAction" method="login">
<result type="json"></result> <!--返回值類型設置為json,不設置返回頁面-->
</action>
</package>
</struts>
配置好後,我們再根據<action>標簽內容來編寫action。方法為method對應的login,類名為loginAction,
注意:包繼承為:json-default ,輸出結果類型為json
如下:
View Code
public class loginAction extends ActionSupport implements
ServletRequestAware,ServletResponseAware {
/**
*
*/
private static final long serialVersionUID = 1L;

HttpServletRequest request;
HttpServletResponse response;

public void setServletRequest(HttpServletRequest request) {
this.request=request;
}

public void setServletResponse(HttpServletResponse response) {
this.response=response;
}

public void login(){
try {
//HttpServletRequest request =ServletActionContext.getRequest();
// HttpServletResponse response=ServletActionContext.getResponse();
this.response.setContentType("text/html;charset=utf-8");
this.response.setCharacterEncoding("UTF-8");
if(this.request.getParameter("username").equals("123456")){
this.response.getWriter().write("真的很奇怪,日本人!");
}else if(this.request.getParameter("username").equals("zhd")){
this.response.getWriter().write("沒有錯,我就是東子哥!");
}else{
this.response.getWriter().write("我就是東子哥!");
}

//將要返回的實體對象進行json處理
// JSONObject json=JSONObject.fromObject(this.getUsername());
//輸出格式如:{"id":1, "username":"zhangsan", "pwd":"123"}
// System.out.println(json);

// this.response.getWriter().write(json.toString());
/**
JSONObject json=new JSONObject();
json.put("login", "login");
response.setContentType("text/html;charset=utf-8");
System.out.println(json);
byte[] jsonBytes = json.toString().getBytes("utf-8");
response.setContentLength(jsonBytes.length);
response.getOutputStream().write(jsonBytes);
**/
/**
JSONObject json=new JSONObject();
json.put("login", "login");
byte[] jsonBytes = json.toString().getBytes("utf-8");
response.setContentType("text/html;charset=utf-8");
response.setContentLength(jsonBytes.length);
response.getOutputStream().write(jsonBytes);
response.getOutputStream().flush();
response.getOutputStream().close();
**/

} catch (Exception e) {
e.printStackTrace();
}
// return null;
}
}

熱點內容
資料庫的演算法 發布:2025-02-05 20:25:32 瀏覽:859
微信解壓異常 發布:2025-02-05 20:24:39 瀏覽:493
linux0位元組文件夾 發布:2025-02-05 20:23:07 瀏覽:652
專題的腳本怎麼寫 發布:2025-02-05 20:19:18 瀏覽:923
獨立站買什麼伺服器 發布:2025-02-05 20:13:24 瀏覽:296
android鬧鍾設置 發布:2025-02-05 20:12:29 瀏覽:955
計算機代碼經典編程 發布:2025-02-05 19:25:09 瀏覽:757
安卓抖音怎麼換不了白色背景 發布:2025-02-05 19:11:16 瀏覽:810
安卓手機如何變成手寫 發布:2025-02-05 19:11:14 瀏覽:981
esp32搭建自己的伺服器 發布:2025-02-05 18:58:00 瀏覽:318