android中的网络通信(二) HTTP网络编程

   上一篇是关于网络底层Socket编程的网络通信,这篇是之上的高级的网络编程协议HTTP协议,也是运用的比较广泛的互联网协议,通常所说的B/S的结构程序就是基于此协议的。
   android中基于HTTP的网络通信的有两种分别是:HttpURLConnection和ApacheHTTP。这篇主要是HttpURLConnection通信。HttpURLConnection通过基于http的url来访问资源进行请求和响应。本篇以一个简单的登陆界面来展示HttpURLConnection。 

   首先在建立android客户端,布局文件分别是输入用户名和密码,传给服务器进行判断,如输入正确则返回成功。布局文件如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="match_parent"
	android:background="#ffffff" android:layout_height="match_parent">
	<TableLayout android:layout_width="fill_parent" android:id="@+id/table"
		android:stretchColumns="1" android:layout_height="wrap_content">
		<TableRow android:layout_width="fill_parent"
			android:layout_height="wrap_content">
			<TextView android:text="name:" android:textColor="#000000"
				android:textSize="20dp" android:layout_width="wrap_content"
				android:layout_height="wrap_content" />
			<EditText android:layout_width="fill_parent"
				android:layout_height="wrap_content" android:id="@+id/name" />
		</TableRow>
		<TableRow android:layout_width="fill_parent"
			android:layout_height="wrap_content">
			<TextView android:text="password:" android:textColor="#000000"
				android:textSize="20dp" android:layout_width="wrap_content"
				android:layout_height="wrap_content" />
			<EditText android:layout_width="fill_parent"
				android:layout_height="wrap_content" android:id="@+id/password" />
		</TableRow>

	</TableLayout>
	<Button android:layout_below="@id/table" android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:id="@+id/submit"
		android:text="submit" />
</RelativeLayout>
java文件通过url来访问服务器的LoginServlet通过LoginServlet来进行判断

/**
 * 
 */
package com.test;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

/**
 * @author liguiwu
 * 
 */
public class login extends Activity {

	private EditText name;
	private EditText password;
	private Button submit;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		name = (EditText) findViewById(R.id.name);
		password = (EditText) findViewById(R.id.password);
		submit = (Button) findViewById(R.id.submit);

		submit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String getName = name.getText().toString();
				String getPassword = password.getText().toString();

				login(getName, getPassword);

			}
		});
	}

	public void login(String name, String password) {
		String urlStr = "http://192.168.1.101:8080/MyServer/servlet/LoginServlet?";
		String queryString = "name=" + name + "&password=" + password;
		urlStr = urlStr + queryString;

		try {
			URL url = new URL(urlStr);
			// 获得http连接的实例
			HttpURLConnection conne = (HttpURLConnection) url.openConnection();
			// 判断是否请求成功
			if (conne.getResponseCode() == HttpURLConnection.HTTP_OK) {
				InputStream in = conne.getInputStream();
				byte[] b = new byte[in.available()];
				// 将b读到缓冲区
				in.read(b);
				String msg = new String(b);
				AlertDialog.Builder build = new AlertDialog.Builder(this);
				build.setMessage(msg).setPositiveButton("确定",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub

							}
						});
				AlertDialog alert = build.create();
				alert.show();
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

	}

}
在Myeclipse新建LoginServlet的servlet类来进行判断,通过apache作为服务器,运行的时候先配置apache,我测试的时候由于忘了配置纠结了好久,测试的时候可以再浏览器中输入客户端的url看是否服务端正常启动。

package com.test;

import java.io.IOException;
import java.io.PrintWriter;

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

public class LoginServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public LoginServlet() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		String msg = null;
		if (name.equals("admin") && password.equals("admin")) {
			msg = "successful";
		} else {
			msg = "fail";
		}
		// 传信息给客户端
		out.print(msg);
		// 刷新输出流
		out.flush();
		out.close();
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);

	}


}
运行结果,返回“successful”



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值