Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 初級開發 >> Android 與Struts2 服務器 交互

Android 與Struts2 服務器 交互

編輯:初級開發

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
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved