1、android從伺服器取出來的所有數據怎麼賦值給textview
Android讀寫資料庫代碼比較多,以下為基本步驟:
創建資料庫,並讀寫</ol>創建一個名為Test的資料庫,並返回一個SQLiteDatabase對象=this.openOrCreateDatabase("Test",MODE_PRIVATE,null);通過execSQL方法來執行一條SQL語句。String CREATE_TABLE="create table 表名(列名,列名,……)";mSQLiteDatabase.execSQL(CREATE_TABLE);
2.以使用insert方法來添加數據,但是insert方法要求把數據都打包到ContentValues中,ContentValues其實就是一個Map,Key值是欄位名稱,Value值是欄位的值。通過ContentValues的put方法就可以把數據放到ContentValues對象中,然後插入到表中去。具體實現如下:<pre t="code" l="java">ContentValues cv=new ContentValues();
cv.put(TABLE_NUM,1);
cv.put(TABLE_DATA,"測試資料庫數據");
mSQLiteDatabase.insert(Test,null,cv);
//同樣可以使用execSQL方法來執行一條「插入「的SQL語句
String INSERT_DATA="insert into 表名(列名,……) values (值,……)";
mSQLiteDatabase.execSQL(INSERT_DATA);
3.創建TextView對象,並賦值TextView textView = (TextView) finadViewById(R.id.textView);textView.setTextView(text);
2、android通過什麼時候去從伺服器端獲取數據
從伺服器獲取數據可以用兩種方式,使用HTTP協議訪問伺服器預留的介面,接收伺服器返回的版數據,這種權方法使用WEB伺服器。另一種是建立socket連接,與伺服器通信。
通信過程中要遵守傳輸協議,HTTP方式要遵守HTTP協議,socket形式底層遵守TCP/IP協議,應用層可以自己定義通訊協議。有一些成熟的協議。SOAP,XMPP等使用XML方式傳輸數據。
3、android開發用什麼從伺服器獲取數據
在android中有時候我們不需要用到本機的SQLite資料庫提供數據,更多的時候是從網路上獲取數據,那麼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;
}
}
運行查看下:http://localhost:8080/PDAServer/login.action?username=123456 當然你可以輸入其他參數的URL
4、android平台的app、手機客戶端和後台伺服器怎麼進行數據交互?
首先不要管安卓端還是蘋果端,現在一般都是響應式的app,你放到安卓或者蘋果或者pc或者平板都是沒有問題的。一般採用的是http介面通訊,或者socket連接。具體你要去查資料找Demo了。而且現在主流是採用html5開發或者混合開發了。所以最好是伺服器提供appAPI介面,通過http訪問伺服器,獲取數據,數據一般是json,或者xml,拿到後解析數據就可以了,然後再用UI框架或者其他框架或者自定義的UI封裝下格式很漂亮了,至於cookie和session等,看你的習慣,網路驗證和簽名那些也自己看習慣,如果涉及到大數據,還需要引入第三方框架的,直接引入就可以了,不過推薦自己寫,防止侵權。都是很通用的。
5、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;
}
}
6、Android開發 獲取伺服器端的數據並顯示在手機的客戶端上
那個控制項都要用,你覺得一個APP一個控制項能搞出來?
可能我理解的你意思有誤,你應該是想了解ListView這個控制項吧,列表控制項
7、android 怎麼實現從伺服器獲取數據更新listview
這個,你使用AsyncTask來進行網路請求,然後獲取數據交給listview的適配器adapter。
如果你是新聞,有很多信息,建議你使用webview載入html的形式,直接訪問網路的url
8、Android通過OKhttp從伺服器端獲取數據
①簡單的非同步Get請求
第一步,創建OKHttpClient對象
第二步,創建Request請求
第三步,創建一個Call對象
第四步,將請求添加到調度中
不多說,直接上代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//okHttp的基本使用 --- get方法
String url = "https://api.douban.com/v2/movie/top250?start=0&count=10";
//1,創建OKHttpClient對象
OkHttpClient mOkHttpClient = new OkHttpClient();
//2,創建一個Request
Request request = new Request.Builder().url(url).build();
//3,創建一個call對象
Call call = mOkHttpClient.newCall(request);
//4,將請求添加到調度中
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
if (response.isSuccessful()) {
final String message = response.body().string();
handler.post(new Runnable() {
@Override
public void run() {
tv_message.setText(message);
progressBar.setVisibility(View.GONE);
}
});
}
}
});
9、android開發中,如何連接伺服器,從伺服器讀取到數據
答:
一,簡單應用 ,用 http 連接就可以。 就是用httpurlconnection 包就行
二,erp 應用,用 webservice,android 調用 webservice 實現連接伺服器。
三,我是.net和 java通吃,…^-^,還用一種辦法,就是用 .net framwork里的 WCF 也可以與android 通信,原理和webservice 差不多。
求加分。。。。。。。。