服务器端代码:
testServlet.java
package com.liuxiang;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class testServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("UTF-8");
String name=request.getParameter("name");
String pwd=request.getParameter("pwd");
String message = ":Welcome to android!";
if (name.equals("admin")&&pwd.equals("liuxiang"))
response.getWriter().write(name+message);
else
response.getWriter().write("Input Error!");
}
}
Android 客户端代码:
HttpAndroidActivity.java
package liuxiang.com.http;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
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.util.EntityUtils;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class HttpAndroidActivity extends Activity
{
EditText e1, e2;
Button b_get, b_post;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
e1 = (EditText) findViewById(R.id.editText1);
e2 = (EditText) findViewById(R.id.editText2);
b_get = (Button) findViewById(R.id.buttonget);
b_get.setOnClickListener(new OnClickListener() // GET 请求
{
@Override
public void onClick(View v)
{
String url = "http://10.0.2.2:8080/aboutAndroid/servlet/testServlet";
url = url+"?name="+e1.getText().toString()+"&pwd="+e2.getText().toString();
HttpClient client = new DefaultHttpClient();
try
{
HttpPost httpPost = new HttpPost(new URI(url));
HttpResponse httpResponse = client.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
String out = EntityUtils.toString(httpEntity);
new AlertDialog.Builder(HttpAndroidActivity.this).setMessage(out).create().show();
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
b_post = (Button) findViewById(R.id.buttonpost);
b_post.setOnClickListener(new OnClickListener() // POST 请求
{
@Override
public void onClick(View arg0)
{
String url = "http://10.0.2.2:8080/aboutAndroid/servlet/testServlet";
String name = e1.getText().toString();
String pwd = e2.getText().toString();
NameValuePair nameValuePair1 = new BasicNameValuePair("name", name);
NameValuePair nameValuePair2 = new BasicNameValuePair("pwd", pwd);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(nameValuePair1);
nameValuePairs.add(nameValuePair2);
try
{
HttpEntity requestHttpEntity = new UrlEncodedFormEntity(nameValuePairs);
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(requestHttpEntity);
HttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
String out = EntityUtils.toString(httpEntity);
new AlertDialog.Builder(HttpAndroidActivity.this).setMessage(out).create().show();
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
mail.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="输入进入服务器的用户名和密码:" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/buttonget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get进入" />
<Button
android:id="@+id/buttonpost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Post进入" />
</LinearLayout>
附件下载:Android与服务器交互