網上有很多例子來演示Android客戶端和伺服器端數據如何實現交互不過這些例子大多比較繁雜,對於初學者來說這是不利的,現在介紹幾種代碼簡單、邏輯清晰的交互例子,本篇博客介紹第四種: 一、伺服器端: 代碼1:添加名為“AndroidServerServlet.Java”的文件 [java] view ...
網上有很多例子來演示Android客戶端和伺服器端數據如何實現交互不過這些例子大多比較繁雜,對於初學者來說這是不利的,現在介紹幾種代碼簡單、邏輯清晰的交互例子,本篇博客介紹第四種:
一、伺服器端:
代碼1:添加名為“AndroidServerServlet.Java”的文件
[java] view plain copy- package com.ghj.packageofservlet;
- 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 AndroidServerServlet extends HttpServlet {
- private static final long serialVersionUID = 6792396567928634227L;
- public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
- response.setContentType("text/plain; charset=UTF-8");
- request.setCharacterEncoding("UTF-8");
- System.err.println(request.getParameter("clientData"));
- PrintWriter printWriter = response.getWriter();
- printWriter.print("您好Android客戶端!");
- printWriter.flush();
- printWriter.close();
- }
- }
代碼2:修改名為“web.xml”的文件
[html] view plain copy- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <servlet>
- <servlet-name>AndroidServerServlet</servlet-name>
- <servlet-class>com.ghj.packageofservlet.AndroidServerServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>AndroidServerServlet</servlet-name>
- <url-pattern>/AndroidServerServlet</url-pattern>
- </servlet-mapping>
- </web-app>
二、Android手機客戶端:
代碼1:下載名為android-async-http-1.4.6.jar的jar文件,下載地址:http://download.csdn.net/download/wangshuxuncom/8116169
代碼2:添加名為“AndroidClientActivity.java”的文件
[java] view plain copy- package com.example.androidclient;
- import org.apache.http.Header;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.Toast;
- import com.loopj.android.http.AsyncHttpClient;
- import com.loopj.android.http.AsyncHttpResponseHandler;
- import com.loopj.android.http.RequestParams;
- public class AndroidClientActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.android_client);
- Button sendButton = (Button) findViewById(R.id.send_button);
- sendButton.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v) {
- RequestParams requestParams = new RequestParams();
- requestParams.add("clientData", "您好伺服器端!");
- new AsyncHttpClient().post("http://172.16.99.207:8080/AndroidServer/AndroidServerServlet", requestParams, new AsyncHttpResponseHandler() {
- @Override
- public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
- if(statusCode == 200){
- Toast.makeText(AndroidClientActivity.this, new String(responseBody), Toast.LENGTH_LONG).show();
- }
- }
- @Override
- public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
- Toast.makeText(AndroidClientActivity.this, "沒有獲取到Android伺服器端的響應!", Toast.LENGTH_LONG).show();
- }
- });
- }
- });
- }
- }
代碼3:添加名為“android_client.xml”的文件
[html] view plain copy- <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" >
- <Button
- android:id="@+id/send_button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:layout_centerVertical="true"
- android:text="@string/hello_server" />
- </RelativeLayout>
說明:使用android-async-http-1.4.6.jar jar這種形式實現Android客戶端和伺服器端數據交互是最常用的。