Android单元测试,模拟http的get和post请求

转自:http://my.oschina.net/songxueyan/blog/147011

不要界面,使用单元测试完成业务逻辑功能

一。配置文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.diandong.tools"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="10" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <uses-library android:name="android.test.runner" />
    </application>
    <instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="com.diandong.tools"
        android:label="Test for my app"/>
 
</manifest>


 

说明:

9行:增加访问网络权限

15行:允许单元测试

17~20行:配置单元测试  targetPackage需要和 <manifest>标签中的   package= 一致


二。Android get和post方法:

package com.diandong.tools;
 
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
 
public class SendGetOrPost {
    public String sendGet(String url) {
       String result ="";
       //url = "http://10.0.2.2/xmlhttp/testmysql.php?email=931918906@qq.com";
       //虚拟机把自身当作localhost或者127.0.0.1 访问电脑的localhost 使用10.0.2.2
       HttpGet httpGet = new HttpGet(url);
       try {
            HttpResponse httpResponse = new DefaultHttpClient().execute(httpGet);
            if (httpResponse.getStatusLine().getStatusCode() == 200)
               {//第三步,使用getEntity方法活得返回结果
                   result = EntityUtils.toString(httpResponse.getEntity());
                }else{
                    result = httpResponse.getStatusLine().toString();
                }
       }catch (Exception e) {
           //System.out.println("error:");
           System.out.println(e);
       }
       return result;
 
    }
    public String sendPost(String url,List<NameValuePair> params) {
        String result = "";
        //url = "http://10.0.2.2/xmlhttp/insertuser.php";
        try {
            HttpPost httpPost = new HttpPost(url);
            //post 需要使用List<NameValuePair>形式的参数
            httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
            HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);
            if (httpResponse.getStatusLine().getStatusCode() == 200)
               {//使用getEntity方法活得返回结果
                   result = EntityUtils.toString(httpResponse.getEntity());
                }else{
                    result = httpResponse.getStatusLine().toString();
                }
        } catch (Exception e) {
            System.out.println(e);
        }
        return result;
    }
}


 

三。测试类

package com.diandong.tools;
 
import java.util.ArrayList;
import java.util.List;
 
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
 
import android.test.AndroidTestCase;
 
public class MyTest extends AndroidTestCase{
    public void testGet() throws Throwable{
        SendGetOrPost sendGetOrPost = new SendGetOrPost();
        String url = "http://10.0.2.2/xmlhttp/testmysql.php?email=931918906@qq.com";
        //虚拟机把自身当作localhost或者127.0.0.1 访问电脑的localhost 使用10.0.2.2
        String result = sendGetOrPost.sendGet(url);
        System.out.println(result);
    }
    public void testPost() throws Throwable{
        SendGetOrPost sendGetOrPost = new SendGetOrPost();
        String url = "http://10.0.2.2/xmlhttp/insertuser.php";
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("email", "smile645@sinaa.cn"));
        params.add(new BasicNameValuePair("pass", "e10adc3949ba59abbe56e057f20f883e"));//123456的md5
        String result =sendGetOrPost.sendPost(url, params);
        System.out.println(result);
    }
 
}

四。window-->show view -->outline中运行测试  run as android junit test

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值