Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android編程中HTTP服務用法詳解

Android編程中HTTP服務用法詳解

編輯:關於Android編程

本文實例講述了Android編程中HTTP服務用法。分享給大家供大家參考,具體如下:

在Android中,除了使用java.net包下的API訪問HTTP服務之外,我們還可以換一種途徑去完成工作。Android SDK附帶了Apache的HttpClient API。Apache HttpClient是一個完善的HTTP客戶端,它提供了對HTTP協議的全面支持,可以使用HTTP GET和POST進行訪問。下面我們就結合實例,介紹一下HttpClient的使用方法。

我們新建一個http項目,項目結構如圖:

在這個項目中,我們不需要任何的Activity,所有的操作都在單元測試類HttpTest.java中完成。

因為使用到了單元測試,所以在這裡先介紹一下如何配置Android中的單元測試。所有配置信息均在AndroidManifest.xml中完成:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.scott.http"
   android:versionCode="1"
   android:versionName="1.0">
  <application android:icon="@drawable/icon" android:label="@string/app_name">
    <!-- 配置測試要使用的類庫 -->
    <uses-library android:name="android.test.runner"/>
  </application>
  <!-- 配置測試設備的主類和目標包 -->
  <instrumentation android:name="android.test.InstrumentationTestRunner"
           android:targetPackage="com.scott.http"/>
  <!-- 訪問HTTP服務所需的網絡權限 -->
  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-sdk android:minSdkVersion="8" />
</manifest>

然後,我們的單元測試類需要繼承android.test.AndroidTestCase類,這個類本身是繼承junit.framework.TestCase,並提供了getContext()方法,用於獲取Android上下文環境,這個設計非常有用,因為很多Android API都是需要Context才能完成的。

現在讓我們來看一下我們的測試用例,HttpTest.java代碼如下:

package com.scot.http.test;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import junit.framework.Assert;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.InputStreamBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.test.AndroidTestCase;
public class HttpTest extends AndroidTestCase {
  private static final String PATH = "http://192.168.1.57:8080/web";
  public void testGet() throws Exception {
    HttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet(PATH + "/TestServlet?id=1001&name=john&age=60");
    HttpResponse response = client.execute(get);
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
      InputStream is = response.getEntity().getContent();
      String result = inStream2String(is);
      Assert.assertEquals(result, "GET_SUCCESS");
    }
  }
  public void testPost() throws Exception {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(PATH + "/TestServlet");
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("id", "1001"));
    params.add(new BasicNameValuePair("name", "john"));
    params.add(new BasicNameValuePair("age", "60"));
    HttpEntity formEntity = new UrlEncodedFormEntity(params);
    post.setEntity(formEntity);
    HttpResponse response = client.execute(post);
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
      InputStream is = response.getEntity().getContent();
      String result = inStream2String(is);
      Assert.assertEquals(result, "POST_SUCCESS");
    }
  }
  public void testUpload() throws Exception {
    InputStream is = getContext().getAssets().open("books.xml");
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(PATH + "/UploadServlet");
    InputStreamBody isb = new InputStreamBody(is, "books.xml");
    MultipartEntity multipartEntity = new MultipartEntity();
    multipartEntity.addPart("file", isb);
    multipartEntity.addPart("desc", new StringBody("this is description."));
    post.setEntity(multipartEntity);
    HttpResponse response = client.execute(post);
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
      is = response.getEntity().getContent();
      String result = inStream2String(is);
      Assert.assertEquals(result, "UPLOAD_SUCCESS");
    }
  }
  //將輸入流轉換成字符串
  private String inStream2String(InputStream is) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    int len = -1;
    while ((len = is.read(buf)) != -1) {
      baos.write(buf, 0, len);
    }
    return new String(baos.toByteArray());
  }
}

因為此文件包含三個測試用例,所以我將會逐個介紹一下。

首先,需要注意的是,我們定位服務器地址時使用到了IP,因為這裡不能用localhost,服務端是在windows上運行,而本單元測試運行在Android平台,如果使用localhost就意味著在Android內部去訪問服務,可能是訪問不到的,所以必須用IP來定位服務。

我們先來分析一下testGet測試用例。我們使用了HttpGet,請求參數直接附在URL後面,然後由HttpClient執行GET請求,如果響應成功的話,取得響應內如輸入流,並轉換成字符串,最後判斷是否為GET_SUCCESS。

testGet測試對應服務端Servlet代碼如下:

@Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println("doGet method is called.");
    String id = request.getParameter("id");
    String name = request.getParameter("name");
    String age = request.getParameter("age");
    System.out.println("id:" + id + ", name:" + name + ", age:" + age);
    response.getWriter().write("GET_SUCCESS");
  }

然後再說testPost測試用例。我們使用了HttpPost,URL後面並沒有附帶參數信息,參數信息被包裝成一個由NameValuePair類型組成的集合的形式,然後經過UrlEncodedFormEntity處理後調用HttpPost的setEntity方法進行參數設置,最後由HttpClient執行。

testPost測試對應的服務端代碼如下:

@Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println("doPost method is called.");
    String id = request.getParameter("id");
    String name = request.getParameter("name");
    String age = request.getParameter("age");
    System.out.println("id:" + id + ", name:" + name + ", age:" + age);
    response.getWriter().write("POST_SUCCESS");
  }

上面兩個是最基本的GET請求和POST請求,參數都是文本數據類型,能滿足普通的需求,不過在有的場合例如我們要用到上傳文件的時候,就不能使用基本的GET請求和POST請求了,我們要使用多部件的POST請求。下面介紹一下如何使用多部件POST操作上傳一個文件到服務端。

由於Android附帶的HttpClient版本暫不支持多部件POST請求,所以我們需要用到一個HttpMime開源項目,該組件是專門處理與MIME類型有關的操作。因為HttpMime是包含在HttpComponents 項目中的,所以我們需要去apache官方網站下載HttpComponents,然後把其中的HttpMime.jar包放到項目中去,如圖:

然後,我們觀察testUpload測試用例,我們用HttpMime提供的InputStreamBody處理文件流參數,用StringBody處理普通文本參數,最後把所有類型參數都加入到一個MultipartEntity的實例中,並將這個multipartEntity設置為此次POST請求的參數實體,然後執行POST請求。服務端Servlet代碼如下:

package com.scott.web.servlet;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
@SuppressWarnings("serial")
public class UploadServlet extends HttpServlet {
  @Override
  @SuppressWarnings("rawtypes")
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if (isMultipart) {
      FileItemFactory factory = new DiskFileItemFactory();
      ServletFileUpload upload = new ServletFileUpload(factory);
      try {
        List items = upload.parseRequest(request);
        Iterator iter = items.iterator();
        while (iter.hasNext()) {
          FileItem item = (FileItem) iter.next();
          if (item.isFormField()) {
            //普通文本信息處理
            String paramName = item.getFieldName();
            String paramValue = item.getString();
            System.out.println(paramName + ":" + paramValue);
          } else {
            //上傳文件信息處理
            String fileName = item.getName();
            byte[] data = item.get();
            String filePath = getServletContext().getRealPath("/files") + "/" + fileName;
            FileOutputStream fos = new FileOutputStream(filePath);
            fos.write(data);
            fos.close();
          }
        }
      } catch (FileUploadException e) {
        e.printStackTrace();
      }
    }
    response.getWriter().write("UPLOAD_SUCCESS");
  }
}

服務端使用apache開源項目FileUpload進行處理,所以我們需要commons-fileupload和commons-io這兩個項目的jar包,對服務端開發不太熟悉的朋友可以到網上查找一下相關資料。

介紹完上面的三種不同的情況之後,我們需要考慮一個問題,在實際應用中,我們不能每次都新建HttpClient,而是應該只為整個應用創建一個HttpClient,並將其用於所有HTTP通信。此外,還應該注意在通過一個HttpClient同時發出多個請求時可能發生的多線程問題。針對這兩個問題,我們需要改進一下我們的項目:

1.擴展系統默認的Application,並應用在項目中。

2.使用HttpClient類庫提供的ThreadSafeClientManager來創建和管理HttpClient。

改進後的項目結構如圖:

其中MyApplication擴展了系統的Application,代碼如下:

package com.scott.http;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import android.app.Application;
public class MyApplication extends Application {
  private HttpClient httpClient;
  @Override
  public void onCreate() {
    super.onCreate();
    httpClient = this.createHttpClient();
  }
  @Override
  public void onLowMemory() {
    super.onLowMemory();
    this.shutdownHttpClient();
  }
  @Override
  public void onTerminate() {
    super.onTerminate();
    this.shutdownHttpClient();
  }
  //創建HttpClient實例
  private HttpClient createHttpClient() {
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
    HttpProtocolParams.setUseExpectContinue(params, true);
    SchemeRegistry schReg = new SchemeRegistry();
    schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ClientConnectionManager connMgr = new ThreadSafeClientConnManager(params, schReg);
    return new DefaultHttpClient(connMgr, params);
  }
  //關閉連接管理器並釋放資源
  private void shutdownHttpClient() {
    if (httpClient != null && httpClient.getConnectionManager() != null) {
      httpClient.getConnectionManager().shutdown();
    }
  }
  //對外提供HttpClient實例
  public HttpClient getHttpClient() {
    return httpClient;
  }
}

我們重寫了onCreate()方法,在系統啟動時就創建一個HttpClient;重寫了onLowMemory()和onTerminate()方法,在內存不足和應用結束時關閉連接,釋放資源。需要注意的是,當實例化DefaultHttpClient時,傳入一個由ThreadSafeClientConnManager創建的一個ClientConnectionManager實例,負責管理HttpClient的HTTP連接。

然後,想要讓我們這個加強版的“Application”生效,需要在AndroidManifest.xml中做如下配置:

<application android:name=".MyApplication" ...>
...
</application>

如果我們沒有配置,系統默認會使用android.app.Application,我們添加了配置,系統就會使用我們的com.scott.http.MyApplication,然後就可以在context中調用getApplication()來獲取MyApplication實例。

有了上面的配置,我們就可以在活動中應用了,HttpActivity.java代碼如下:

package com.scott.http;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class HttpActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button btn = (Button) findViewById(R.id.btn);
    btn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        execute();
      }
    });
  }
  private void execute() {
    try {
      MyApplication app = (MyApplication) this.getApplication(); //獲取MyApplication實例
      HttpClient client = app.getHttpClient();  //獲取HttpClient實例
      HttpGet get = new HttpGet("http://192.168.1.57:8080/web/TestServlet?id=1001&name=john&age=60");
      HttpResponse response = client.execute(get);
      if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        InputStream is = response.getEntity().getContent();
        String result = inStream2String(is);
        Toast.makeText(this, result, Toast.LENGTH_LONG).show();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  //將輸入流轉換成字符串
  private String inStream2String(InputStream is) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    int len = -1;
    while ((len = is.read(buf)) != -1) {
      baos.write(buf, 0, len);
    }
    return new String(baos.toByteArray());
  }
}

點擊“execute”按鈕,執行結果如下:

希望本文所述對大家Android程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved