这是我第一次写Android与服务器端数据交互的案例,新手请大神莫喷哈。本案例是一个最简单的访问服务器的,入门级的。后台服务器采用JSP+servlet技术通过JSP可以测试后台服务器是否能够正确运行。当然以后牛逼了可以写struts2 hibernate+spring的但是任何牛人都是从初级入门的,废话就不多说了。下面贴出所有的步骤和案例代码;
下载该案例所有代码
在Eclipse中新建一个dynamic web项目 然后再新建一个servlet;我取名为Login:
代码如下:package com;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Login
*/
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public Login() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("UTF-8");
response.getWriter().write("欢迎你:"+request.getParameter("username"));
}
}
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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>AndroidJSP</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>Login</display-name>
<servlet-name>Login</servlet-name>
<servlet-class>com.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/Login</url-pattern>
</servlet-mapping>
</web-app>
浏览器端测试的jsp页面代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="http://180.84.33.181:8080/AndroidJSP/Login?username=123456">提交信息</a>
</body>
</html>
我电脑的Ip地址是180.84.33.181当然你可以根据自己的IP修改 也可以直接把IP改为localhost,但是后面要用Android App访问我建议你还是采用本机的IP,浏览器端启动和截图分别如下:
点击提交后的运行结果为:
好了,到此我们可以测试出服务器端的程序没有问题了,接下来写Android端程序。
为了简便操作我直接在Android端页面做了一个按钮,当点击按钮后即把数据发到服务器端,再通过服务器端把数据返回到Android客户端。
Android布局文件:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是一个访问服务器的APP" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginRight="26dp"
android:layout_marginTop="116dp"
android:text="我是zhangsan" />
</RelativeLayout>
activity如下:
package com.example.androidjsp;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
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.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private static String url="http://180.84.33.181:8080/AndroidJSP/Login";
private static Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=(Button)findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO 自动生成的方法存根
getPDAServerData(url);
}
});
}
/**
* 请求服务
* @param url
*/
private void getPDAServerData(String url){
url+="?username=zhangsan";
HttpClient client=new DefaultHttpClient();
HttpPost request;
try {
request = new HttpPost(new URI(url));
HttpResponse response=client.execute(request);
//判断请求是否成功
if(response.getStatusLine().getStatusCode()==200){
HttpEntity entity=response.getEntity();
if(entity!=null){
String out=EntityUtils.toString(entity);
new AlertDialog.Builder(this).setMessage(out).create().show();
}
}
}catch (URISyntaxException e) {
e.printStackTrace();
}
catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
特别说明一下private static String url="http://180.84.33.181:8080/AndroidJSP/Login";里面的这个IP地址一定要和服务器的IP地址一致哈!!!
因为要用到网络,所以要在配置文件中假如接入网络的权限:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidjsp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.androidjsp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
<uses-permission android:name="android.permission.INTERNET" />这个就是加入后的权限
到此Android端的代码全部写完了,
这是启动界面的图片,当点击按钮式即出现如下的结果
有什么不懂得可以问我哈
下载该案例所有代码
在Eclipse中新建一个dynamic web项目 然后再新建一个servlet;我取名为Login:
代码如下:package com;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Login
*/
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public Login() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("UTF-8");
response.getWriter().write("欢迎你:"+request.getParameter("username"));
}
}
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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>AndroidJSP</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>Login</display-name>
<servlet-name>Login</servlet-name>
<servlet-class>com.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/Login</url-pattern>
</servlet-mapping>
</web-app>
浏览器端测试的jsp页面代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="http://180.84.33.181:8080/AndroidJSP/Login?username=123456">提交信息</a>
</body>
</html>
我电脑的Ip地址是180.84.33.181当然你可以根据自己的IP修改 也可以直接把IP改为localhost,但是后面要用Android App访问我建议你还是采用本机的IP,浏览器端启动和截图分别如下:
点击提交后的运行结果为:
好了,到此我们可以测试出服务器端的程序没有问题了,接下来写Android端程序。
为了简便操作我直接在Android端页面做了一个按钮,当点击按钮后即把数据发到服务器端,再通过服务器端把数据返回到Android客户端。
Android布局文件:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是一个访问服务器的APP" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginRight="26dp"
android:layout_marginTop="116dp"
android:text="我是zhangsan" />
</RelativeLayout>
activity如下:
package com.example.androidjsp;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
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.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private static String url="http://180.84.33.181:8080/AndroidJSP/Login";
private static Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=(Button)findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO 自动生成的方法存根
getPDAServerData(url);
}
});
}
/**
* 请求服务
* @param url
*/
private void getPDAServerData(String url){
url+="?username=zhangsan";
HttpClient client=new DefaultHttpClient();
HttpPost request;
try {
request = new HttpPost(new URI(url));
HttpResponse response=client.execute(request);
//判断请求是否成功
if(response.getStatusLine().getStatusCode()==200){
HttpEntity entity=response.getEntity();
if(entity!=null){
String out=EntityUtils.toString(entity);
new AlertDialog.Builder(this).setMessage(out).create().show();
}
}
}catch (URISyntaxException e) {
e.printStackTrace();
}
catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
特别说明一下private static String url="http://180.84.33.181:8080/AndroidJSP/Login";里面的这个IP地址一定要和服务器的IP地址一致哈!!!
因为要用到网络,所以要在配置文件中假如接入网络的权限:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidjsp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.androidjsp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
<uses-permission android:name="android.permission.INTERNET" />这个就是加入后的权限
到此Android端的代码全部写完了,
这是启动界面的图片,当点击按钮式即出现如下的结果
有什么不懂得可以问我哈