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中完成:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

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





然后,我们的单元测试类需要继承android.test.AndroidTestCase类,这个类本身是继承junit.framework.TestCase,并提供了getContext()方法,用于获取Android上下文环境,这个设计非常有用,因为很多Android API都是需要Context才能完成的。
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()); 
  } 
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值