標籤:
首先,伺服器端已經存在一個項目,該項目基於SSH,已經部署在tomcat上並且正在運行了.我在這個項目的action層建立了一個包,該包主要是Struts2的一些action,只是這些action不是返回某些view,而是返回json,定義這些action的XML配置如下:
<package name="mobie" namespace="/mobie" extends="json-default"> <action name="login" class="loginAction" method="login"> <!-- 設定傳回型別為json --> <result name="success" type="json"> <param name="includeProperties"> user\.id,user\.email,user\.password,user\.gender,user\.tel,user\.nickname,user\.isActive </param> </result> </action> </package>
然後主要開發android端,在android端我的思路大概是這樣的:通過http協議訪問伺服器端的action,伺服器返回json,然後android接受json後,通過解析json構造出執行個體,然後顯示在activity上反饋給使用者.於是,在android端的主要工作就是解析json和顯示資料的問題了,而解析json的工作可以通過某些包來做比較方便.
建立android項目:匯入一下jar包:
介面檔案:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.ddd3.ddd3.MainActivity" android:orientation="vertical" android:background="@color/colorBackground"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="JeanseLam?" android:textSize="35dp" android:textColor="@color/colorText"/> <View android:layout_width="match_parent" android:layout_height="1dip" android:background="#696969" android:layout_marginTop="5dp" android:layout_marginBottom="5dp"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/mainUrl" android:textSize="20dp" android:textColor="@color/colorText"/> <View android:layout_width="fill_parent" android:layout_height="40dp"></View> <TextView android:id="@+id/loginInfo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="15dp" android:textColor="@color/colorError"/> <EditText android:id="@+id/email" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:hint="Type your Email"/> <EditText android:id="@+id/password" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:password="true" android:hint="Enter password"/> <Button android:id="@+id/btn_login" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/button_selector" android:text="登入" android:textColor="@color/colorBtnText" android:layout_marginBottom="10dp"/> <Button android:id="@+id/btn_exit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="退出" android:background="@drawable/button_selector" android:textColor="@color/colorBtnText" android:layout_marginBottom="10dp"/> <TextView android:id="@+id/btn_register" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="新使用者?" android:textColor="@color/colorText" android:autoLink="all"/></LinearLayout>
activity:
package com.ddd3.ddd3;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import com.android.volley.DefaultRetryPolicy;import com.android.volley.Request;import com.android.volley.RequestQueue;import com.android.volley.Response;import com.android.volley.VolleyError;import com.android.volley.toolbox.JsonObjectRequest;import com.android.volley.toolbox.StringRequest;import com.android.volley.toolbox.Volley;import com.ddd3.entity.User;import com.ddd3.service.JSONService;public class MainActivity extends AppCompatActivity { private EditText emailText; private EditText passwordText; private Button btn_login; private Button btn_exit; private TextView info; private RequestQueue requestQueue;//用於網路網路請求,每一個activity只需要一個requestQueue //private UserService userService; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView(){ emailText = (EditText)findViewById(R.id.email); passwordText = (EditText)findViewById(R.id.password); btn_login = (Button)findViewById(R.id.btn_login); btn_exit = (Button)findViewById(R.id.btn_exit); info = (TextView)findViewById(R.id.loginInfo); btn_login.setOnClickListener(new ButtonListener()); btn_exit.setOnClickListener(new ButtonListener()); requestQueue = Volley.newRequestQueue(this);//執行個體化請求隊列 } class ButtonListener implements View.OnClickListener{ @Override public void onClick(View v) { if(v.getId() == btn_login.getId()){ String email = emailText.getText().toString(); String password = passwordText.getText().toString(); if(email.equals("") || password.equals("")){ info.setText("請輸入您的Email和密碼"); } else{ //服務端url String url = "http://192.168.56.1:8080/JeanseLam/mobie/login?email="+email+"&password="+password; JsonObjectRequest jr = new JsonObjectRequest(Request.Method.GET,url, null, new Response.Listener<org.json.JSONObject>() { @Override public void onResponse(org.json.JSONObject jsonObject) { System.out.println("------------------" + jsonObject.toString()); User user = (User)JSONService.jsonString2Object(jsonObject.toString(), User.class); System.out.println(user.getEmail()+user.getNickname()); /** *跳轉或者顯示 */ } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { System.out.println("-------------------"+error.getMessage()); } }); requestQueue.add(jr); /*** StringRequest stringRequest = new StringRequest("http://192.168.56.1:8080/JeanseLam", new Response.Listener<String>() { @Override public void onResponse(String response) { System.out.println("SUCCESS---------------->"+response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { System.out.println("ERROR------------------>"+error.getMessage()); } }); stringRequest.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 1, 1.0f)); requestQueue.add(stringRequest); */ } } else if(v.getId() == btn_exit.getId()){ android.os.Process.killProcess(android.os.Process.myPid()); } } }}
需要注意的是:由於訪問本地項目,url的地址為:192.168.56.1:8080而不是localhost或其他
android用戶端訪問服務端tomcat