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

一。配置文件:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 <manifestxmlns: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-permissionandroid:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> <uses-libraryandroid: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方法:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 packagecom.diandong.tools; importjava.util.List; importorg.apache.http.HttpResponse; importorg.apache.http.NameValuePair; importorg.apache.http.client.entity.UrlEncodedFormEntity; importorg.apache.http.client.methods.HttpGet; importorg.apache.http.client.methods.HttpPost; importorg.apache.http.impl.client.DefaultHttpClient; importorg.apache.http.protocol.HTTP; importorg.apache.http.util.EntityUtils; publicclassSendGetOrPost { publicString 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 =newHttpGet(url); try{ HttpResponse httpResponse =newDefaultHttpClient().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); } returnresult; } publicString sendPost(String url,List<NameValuePair> params) { String result =""; //url = "http://10.0.2.2/xmlhttp/insertuser.php"; try{ HttpPost httpPost =newHttpPost(url); //post 需要使用List<NameValuePair>形式的参数 httpPost.setEntity(newUrlEncodedFormEntity(params,HTTP.UTF_8)); HttpResponse httpResponse =newDefaultHttpClient().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); } returnresult; } }

三。测试类

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 packagecom.diandong.tools; importjava.util.ArrayList; importjava.util.List; importorg.apache.http.NameValuePair; importorg.apache.http.message.BasicNameValuePair; importandroid.test.AndroidTestCase; publicclassMyTestextendsAndroidTestCase{ publicvoidtestGet()throwsThrowable{ SendGetOrPost sendGetOrPost =newSendGetOrPost(); 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); } publicvoidtestPost()throwsThrowable{ SendGetOrPost sendGetOrPost =newSendGetOrPost(); String url ="http://10.0.2.2/xmlhttp/insertuser.php"; List<NameValuePair> params =newArrayList<NameValuePair>(); params.add(newBasicNameValuePair("email","smile645@sinaa.cn")); params.add(newBasicNameValuePair("pass","e10adc3949ba59abbe56e057f20f883e"));//123456的md5 String result =sendGetOrPost.sendPost(url, params); System.out.println(result); } }

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


五。遇到的错误以及解决方法:

1.android中访问localhost报错参考这里

2.Wamp 报403 You don't have permission to access 参考这里

3.Android 看不到输出信息System.out.println 参考这里

更多相关文章

  1. Android(安卓)---- WebView与JavaScript交互调用(2)
  2. Android——Fragment介绍及两种基本使用方法
  3. 基于Android中获取资源的id和url方法总结
  4. 浅入浅出Android(016):分别使用WebView和Intent访问网页
  5. Android(安卓)intent.setFlags方法中的参数值含义
  6. "android sdk Content Loader's has encountered a problem"的解
  7. Android(安卓)教程 翻译 1 Activities 活动
  8. Android(安卓)事件分发机制总结篇
  9. Android中AutoCompleteTextView完整示例(一)

随机推荐

  1. Android(安卓)游戏与应用开发最佳学习路
  2. android 使用gdb调试的方式
  3. Android(安卓)软键盘问题总结
  4. Android(安卓)获取网络时间
  5. [置顶] Android4.2.2自增物理按键(framewo
  6. 关于cocos2dx的eclipse的"serializing cd
  7. PullToRefresh的简单使用
  8. android使用全局变量的两种方法
  9. android之实现底部TabHost
  10. Android文件浏览器的开发