編輯:初級開發
Struts與android交互,看別人寫的天花亂墜,簡單才是王道,使用http交互
1. struts的開發,不多講了,只些大致的步驟
a) 引入struts的jar包
b) 配置web.XML
c) 配置strust.XML
d) 創建Action
Web.XML
<?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
<?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>
<package name="littlefu" extends="struts-default">
<action name="login" method="login" class="com.android.action.LoginAction">
<result name="success">
</result>
</action>
</package>
</struts>
LoginAction.Java
public class LoginAction extends ActionSupport implements
ServletRequestAware,ServletResponseAware {
HttpServletRequest request;
HttpServletResponse response;
public void login(){
try {
if("android".endsWith(request.getParameter("username"))){
response.getWriter().write("hello android, I am waiting for you");
}
} catch (IOException e) {
e.printStackTrace();
}
public void setServletRequest(HttpServletRequest arg0) {
this.request = arg0;
public void setServletResponse(HttpServletResponse arg0) {
this.response = arg0;
//服務端開發完成,啟動tomcat,測試鏈接,在浏覽器輸入以下網址,頁面返回字符串為“hello android, I am waiting for you”
//http://localhost:8080/Android_V0.1/login.action?username=android
2. 終端開發
a) 建立android Project項目
b) 配置androidMainfext.XML,添加權限(重要)
<uses-permission android:name="android.permission.INTERNET"/>
a) 創建Activity
public class LoginActivity extends Activity {
String uri = "http://58.154.238.141:8080/android_V0.1/login.action";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentVIEw(R.layout.main);
getServerData(uri);
private void getServerData(String uri){
//傳遞參數
uri+="?username=android";
HttpGet request = new HttpGet(uri);
try {
HttpResponse response = new DefaultHttpClIEnt().execute(request);
String backStr = EntityUtils.toString(response.getEntity());
new AlertDialog.Builder(this).setMessage(backStr)
.setPositiveButton("ok", new OnClickListener(){
@Override
public void onClick(DialogInterface arg0,
int arg1) {
}
})
.create().show();
} catch (ClIEntProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//運行就可以了
//顯示彈出對話框,為服務器返回的字符串
原文出處:http://hi.baidu.com/fufengrui/blog/item/1333e08c4b118e04b31bbad4.Html很多初入android或Java開發的新手對Thread、Looper、Handler和Message仍然比較迷惑,衍生的有HandlerThread、Java.uti
Android的服務開發中我們可能都是Activity和Service同進程處理,但想象過系統提供的各種ServiceManager嗎? 比如 SensorManage
這段時間都在看Java,android放了好久,現在慢慢再看先上結果圖:這次首先要實現上面的效果,使用的是ListActivity和SimpleAdapter適配器首先
在游戲開發中,往往要提供選關的頁面,選擇關卡可以簡單地使用listVIEw,如果想效果好一點,可以選擇 用gallery控件。Gallery控件的使用在api demo