android 客户端与web服务器端通信

最近要做一个项目,所以进行了一个小测试.在网上看到的很多Demo,全部是在onCreate()方法中进行连接以及请求服务器端的数据并且在控件中显示的,但是请教了别人之后才知道这种方法现在已经不能用了,必须在主线程中开启一个线程,并且使用Handler这个对象来实现数据的异步请求.然后当请求响应完成之后才会在界面中更新数据,这应该就是android中的异步请求机制吧..下面是整个服务器端以及客户端的代码.首先我服务器端是通过struts来完成的.所以关于struts的jar包以及Json包所依赖的jar包都要导入到web程序中去.以下是服务器端的包图


web.xml文件中要配置过滤器,我是把所有请求都通过struts中的action进行处理的,所以下面是web.xml中的代码

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>YltxServer</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	<!-- 定义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>
</web-app>

同时按照struts中的规则,创建一个struts.xml文件,并注册自己写的action
package com.maylor.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport implements ServletRequestAware,
		ServletResponseAware {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private HttpServletResponse 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");
			// 将要返回的实体对象进行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("username", "username");
			json.put("password", "password");
			JSONObject json1 = new JSONObject();
			json1.put("phone", "phone");
			json1.put("tel", "tel");
			JSONArray array = new JSONArray();
			array.add(json);
			array.add(json1);
			byte[] jsonBytes = array.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;
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		// TODO Auto-generated method stub
		this.response = arg0;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		this.request = arg0;
	}
}
然后就是客户端的请求了.

下面是android客户端的代码,记住要在AndroidMainifest.xml中加入Internet访问权限

<uses-permission android:name="android.permission.INTERNET">

</uses-permission>

下面就是客户端的代码部分了.

package com.maylor.demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
	private static String URL = "http://192.168.2.52:8080/YltxServer/login";
	public Handler mHandler;

	@SuppressLint("HandlerLeak")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		getPDAServerData();
		// Button button = (Button) findViewById(R.id.button1);
		// button.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View arg0) {
		// // TODO Auto-generated method stub
		// Intent intent = new Intent(MainActivity.this,
		// PoiSearchActivity.class);
		// startActivity(intent);
		// }
		// });
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				if (msg.what == 1000) {
					TextView view = (TextView) findViewById(R.id.textview);
					String str = "";
					try {
						JSONArray array = new JSONArray(msg.obj.toString());
						JSONObject object = array.getJSONObject(0);
						str = object.getString("username");
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Log.e("wangchao", str);
					view.setText(str);
				}
			}
		};
	}

	/**
	 * 请求服务
	 * 
	 * @param url
	 */
	private void getPDAServerData() {
		new Thread() {
			public void run() {
				HttpClient client = new DefaultHttpClient();
				HttpGet request;
				String msg = "";
				try {
					request = new HttpGet(URL);
					HttpResponse response = client.execute(request);
					// 判断请求是否成功
					if (response.getStatusLine().getStatusCode() == 200) {
						HttpEntity entity = response.getEntity();
						if (entity != null) {
							InputStream in = entity.getContent();

							BufferedReader buff = new BufferedReader(
									new InputStreamReader(in));
							String line = "";
							while ((line = buff.readLine()) != null) {
								msg += line;
							}
							Message msg1 = mHandler.obtainMessage();
							msg1.what = 1000;
							msg1.obj = msg;
							mHandler.sendMessage(msg1);
						}
					}
				} catch (ClientProtocolException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}.start();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}
这个服务就算是全部完成了..整个工程

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
一个运行于 安卓系统的 小型web服务器,包括php/mysql 配置文件可以修改,另外可以用phpmyadmin 管理数据库。 让你的手机立刻变身小型服务器。 =============================== Turn your Android devices into a web and database server with Palapa Web Server, a suite of web developer. This application has been designed for low memory consumption and CPU usage, specially used for smartphone and tablet. Hey, it's free and you don't need a root access to run Palapa Web Server! # Requirements - Internal memory should not be less than 50MB! - ARM based processor - Minimum Android 2.2 Froyo # Features - Lighttpd 1.4.32 - PHP 5.5.1 - MySQL 5.1.69 - Msmtp 1.4.31 - phpMyAdmin 4.0.4.1 - Web Admin 1.0.1 # Default Document Root - Path : /sdcard/pws/www/ # Default URL - Address : http://127.0.0.1:8080/ # Web Admin Informations - Address : http://127.0.0.1:9999 - Username : admin - Password : admin # MySQL Informations - Host : localhost (127.0.0.1) - Port : 3306 - Username : root - Password : adminadmin # phpMyAdmin Informations - Address : http://127.0.0.1:9999/phpmyadmin - Username : root - Password : adminadmin # Problem? If something is not working properly, please try to restart your phone. # Known Issues As you know, compatibility issue is a big problem of Android phone, I can't test it in all phones. So if it can't work for your phone, just uninstall it, I'm sorry to waste your time. You also can send a mail to let me know your phone model, if I solve it, I will let you know.
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值