【Android系统之Web交换Demo】

一、系统介绍

Android系统发送请求到Web系统,Web系统可以是PHP语言、Python语言、java语言、ASP语言开发的,但是必须满足异构JVM的交换协议,可以是WebService请求、HTTP请求(普通表单请求和Restful格式的json请求)、Socket等请求。

二、系统源码

本例子以Android模拟登录发送数据到Web系统,Web系统采用JAVA语言中的Servlet

Android代码如下:

1)编写xml界面文件

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent">

   

    <TextView  

   android:layout_width="fill_parent" 

   android:layout_height="wrap_content" 

   android:text="@string/username"/>

    <EditText

    android:id="@+id/userName"

    android:layout_width="fill_parent" 

    android:layout_height="wrap_content"/>

    

    <TextView

   android:layout_width="fill_parent" 

   android:layout_height="wrap_content" 

   android:text="@string/password"/>

    

    <EditText

        android:id="@+id/passwd"

    android:layout_width="fill_parent" 

    android:layout_height="wrap_content" />

   

   <Button

    android:id="@+id/ok"

    android:layout_width="fill_parent" 

   android:layout_height="wrap_content" 

   android:text="@string/ok"/> 

   

    <Button  

    android:layout_width="fill_parent" 

   android:layout_height="wrap_content" 

   android:text="@string/cacel"/> 

</LinearLayout>


【Android系统之Web交换Demo】
 

2)编写JAVA注册事件

package cn.com.gaojs.demo;

import java.util.ArrayList;

import java.util.List;

import org.apache.http.NameValuePair;

import org.apache.http.message.BasicNameValuePair;

import cn.com.gaojs.service.impl.NetWorkTools;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

public class LoginActivity extends Activity {

public static final  String url ="http://192.168.1.100:8080/AndroidWeb/servlet/PDAServlet";

    

private Button btn;

private EditText userName;

private EditText passwd;

/** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        

        userName = (EditText) this.findViewById(R.id.userName);

        passwd = (EditText) this.findViewById(R.id.passwd);

        btn = (Button) this.findViewById(R.id.ok);

        btn.setOnClickListener(new LoginOnClickListener());

    }

    

    private class LoginOnClickListener implements OnClickListener{

@Override

public void onClick(View v) {

String userName1 = userName.getText().toString();

String passwd1 = passwd.getText().toString();

Toast.makeText(LoginActivity.this, "Hello"+userName1+passwd1, 10).show();

List<NameValuePair> params = new ArrayList<NameValuePair>();

params.add(new BasicNameValuePair("userName",userName1));

params.add(new BasicNameValuePair("passwd",passwd1));

params.add(new BasicNameValuePair("method","login"));

System.out.println("------begin call---NetWorkTools---");

NetWorkTools.test(params);

System.out.println("------end call---NetWorkTools---");

}

    }

}

3) 封装http请求

package cn.com.gaojs.service.impl;

import java.io.IOException;

import java.io.UnsupportedEncodingException;

import java.util.List;

import org.apache.http.HttpResponse;

import org.apache.http.NameValuePair;

import org.apache.http.ParseException;

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 cn.com.gaojs.demo.LoginActivity;

public class NetWorkTools {

public static void test(List<NameValuePair> params){

try {

System.out.println(params);

HttpPost post = new HttpPost(LoginActivity.url);

//params.add(new BasicNameValuePair("",""));

post.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));

HttpResponse response = new DefaultHttpClient().execute(post);

String content  = EntityUtils.toString(response.getEntity()).trim();

System.out.println(content);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ClientProtocolException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

Web端代码如下:

1)声明servlet

package cn.com.android.servlet;

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 PDAServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

this.doPost(request, response); 

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

String method = request.getParameter("method");

String userName = request.getParameter("userName");

String passwd = request.getParameter("passwd");

System.out.println(method +",username="+userName+",passwd="+passwd);

//此处代码是处理登录逻辑的

if("login".equalsIgnoreCase(method)){

//doLogin(request, response);

}else if ("XXXX".equalsIgnoreCase(method)){

//doXXXX(request, response);

}else if ("YYY".equalsIgnoreCase(method)){

//doYYY(request, response);

}

}

2)Web.xml配置请求路径

<servlet>

    <servlet-name>PDAServlet</servlet-name>

    <servlet-class>cn.com.android.servlet.PDAServlet</servlet-class>

  </servlet>

 

  <servlet-mapping>

    <servlet-name>PDAServlet</servlet-name>

    <url-pattern>/servlet/PDAServlet</url-pattern>

  </servlet-mapping>

3)启动,验证


【Android系统之Web交换Demo】
 

三、验证


【Android系统之Web交换Demo】
 

相关推荐