码迷,mamicode.com
首页 > 移动开发 > 详细

android客户端访问服务端tomcat

时间:2015-11-11 13:27:36      阅读:379      评论:0      收藏:0      [点我收藏+]

标签:

首先,服务器端已经存在一个项目,该项目基于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

标签:

原文地址:http://my.oschina.net/u/2328736/blog/528850

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!