`
chengyu2099
  • 浏览: 474216 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

android apache HTTP demo 互联网访问

 
阅读更多
android 

用户名:________
密  码:________

submitButton,resetButton
(模拟器上访问地址填写
http://10.0.2.2:8080/test/servlet/androidServlet
不用写127.0.0.1或localhost
)
第二部:建立个web项目的一个servlet,接受android请求



package com.isoftstone.cry;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

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

public class LoginActivity extends Activity 
{
	private Button submit , reset ;
	private EditText username , password ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_layout);
		//实例化组件
		username = (EditText)findViewById(R.id.loginName);
		password = (EditText)findViewById(R.id.loginPsw);
		
		submit = (Button)findViewById(R.id.submit);
		reset = (Button)findViewById(R.id.reset);
		
		//添加监听
		reset.setOnClickListener(resetListener);
		submit.setOnClickListener(submitListener);
	}
	
	//resetListener
	private OnClickListener resetListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			username.setText("");
			password.setText("");
		}
	};
	//submitListener
	private OnClickListener submitListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			String _username = username.getText().toString();
			String _password = password.getText().toString();
			Log.i("android servlet", _username+" "+_password);
			
			login(_username,_password);
		}
	};
	//showDialog
	private void showDialog(String msg)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(msg).setCancelable(false)
		.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				
			}
		});
		AlertDialog alert = builder.create();
		alert.show();
	}
	//login method 
	private void login(String _username,String _password)
	{
		String strUrl = "http://10.0.2.2:8080/test/servlet/androidServlet";
		HttpPost request = new HttpPost(strUrl);
		List<NameValuePair> parameters = new ArrayList<NameValuePair>();
		parameters.add(new BasicNameValuePair("name", _username));
		parameters.add(new BasicNameValuePair("psw", _password));
		try {
			request.setEntity(new UrlEncodedFormEntity(parameters,HTTP.UTF_8));
			HttpResponse response = new DefaultHttpClient().execute(request);
			if(response.getStatusLine().getStatusCode()==200){
				String msg = EntityUtils.toString(response.getEntity());
				showDialog(msg);
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >


            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/username" />



            <EditText
                android:id="@+id/loginName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="textPersonName" >

                <requestFocus />
            </EditText>

        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >


            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/password" />


            <EditText
                android:id="@+id/loginPsw"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="textPassword" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >


            <Button
                android:id="@+id/submit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="submit" />



            <Button
                android:id="@+id/reset"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="reset" />

        </TableRow>

    </TableLayout>

</LinearLayout>


添加访问权限
 <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
       <activity 
            android:name=".LoginActivity">
             <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


test项目
package com.isoftstone.cry;

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 androidServlet extends HttpServlet 
{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException 
	{
		System.out.println("****************/servlet/androidServlet*************");
		
		String name = request.getParameter("name");
		String psw = request.getParameter("psw");
		
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		out.print("login success");
		out.flush();
		out.close();
		
	}

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

}

分享到:
评论

相关推荐

    Android客户端与pc服务端程序demo

    - **HTTP通信**:在这个demo中,Android客户端与PC服务端是通过HTTP协议进行通信的。HTTP(超文本传输协议)是互联网上应用最广泛的一种网络协议,用于从万维网服务器传输超文本到本地浏览器的传输协议。 - **...

    海康摄像头demo(自用)

    9. **移动应用兼容**:现代摄像头系统往往也支持移动端访问,通过iOS或Android应用进行控制,这意味着开发者还需要熟悉相应的移动开发平台和框架。 10. **监控平台集成**:在企业级应用中,海康摄像头通常会与专业...

    安卓Ftp服务端Demo

    1. **网络权限**:Android系统对网络访问有严格的限制,开发者需要在AndroidManifest.xml中声明INTERNET权限,否则应用无法建立网络连接。 2. **FTP服务器库**:通常,Android开发者会使用第三方库如Apache Commons...

    网络咨询服务端

    在Android应用开发中,网络咨询服务通常涉及到HTTP和HTTPS协议,它们是互联网上应用最广泛的数据交换协议。HTTP(超文本传输协议)用于传输超媒体信息,如HTML文档;HTTPS(安全套接层超文本传输协议)则是在HTTP的...

    手机wifi热点服务器搭建

    3. **端口映射**:由于安卓的安全机制,可能需要开启USB调试,通过ADB(Android Debug Bridge)将本地端口映射到手机端口,以便外部设备可以通过特定端口访问手机上的服务。 4. **测试与连接**:一旦服务器软件配置...

Global site tag (gtag.js) - Google Analytics