不要界面,使用单元测试完成业务逻辑功能
一。配置文件:
package="com.diandong.tools"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="9"
android:targetSdkVersion="10" />
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.diandong.tools"
android:label="Test for my app"/>
说明:
9行:增加访问网络权限
15行:允许单元测试
17~20行:配置单元测试 targetPackage需要和 标签中的 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 params) {
String result = "";
//url = "http://10.0.2.2/xmlhttp/insertuser.php";
try {
HttpPost httpPost = new HttpPost(url);
//post 需要使用List形式的参数
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 params = new ArrayList();
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