首先要有架包:戳这儿下载
其次要有访问网络的权限:<uses-permissionandroid:name="android.permission.INTERNET"/>
代码:
try{ HttpClient httpClient = new DefaultHttpClient(); String url = "http://v.juhe.cn/weather/index"; HttpGet httpGet = new HttpGet(url); HttpResponse httpResponse = httpClient.execute(httpGet); if(httpResponse.getStatusLine().getStatusCode()==200) { result = EntityUtils.toString(httpResponse.getEntity()); } }catch(Exception e){ e.printStackTrace(); }
如果编译时出现下面的报错的话
Information:Gradle tasks [:app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:assembleDebug]
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAnimatedVectorDrawable2411Library UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72411Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42411Library UP-TO-DATE
:app:prepareComAndroidSupportSupportVectorDrawable2411Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:preDebugAndroidTestBuild UP-TO-DATE
:app:prepareDebugAndroidTestDependencies
:app:compileDebugAndroidTestAidl UP-TO-DATE
:app:processDebugAndroidTestManifest UP-TO-DATE
:app:compileDebugAndroidTestRenderscript UP-TO-DATE
:app:generateDebugAndroidTestBuildConfig UP-TO-DATE
:app:generateDebugAndroidTestAssets UP-TO-DATE
:app:mergeDebugAndroidTestAssets UP-TO-DATE
:app:generateDebugAndroidTestResValues UP-TO-DATE
:app:generateDebugAndroidTestResources UP-TO-DATE
:app:mergeDebugAndroidTestResources UP-TO-DATE
:app:processDebugAndroidTestResources UP-TO-DATE
:app:generateDebugAndroidTestSources UP-TO-DATE
:app:processDebugJavaRes UP-TO-DATE
:app:compileDebugJavaWithJavac UP-TO-DATE
:app:compileDebugNdk UP-TO-DATE
:app:compileDebugSources UP-TO-DATE
:app:preDexDebug UP-TO-DATE
:app:dexDebug UP-TO-DATE
:app:validateDebugSigning
:app:packageDebug
Error:duplicate files during packaging of APK C:\Users\asus1\AndroidStudioProjects\HttpGet\app\build\outputs\apk\app-debug-unaligned.apk
Path in archive: META-INF/LICENSE
Origin 1: C:\Users\asus1\AndroidStudioProjects\HttpGet\app\libs\httpmime-4.5.2.jar
Origin 2: C:\Users\asus1\AndroidStudioProjects\HttpGet\app\libs\httpclient-win-4.5.2.jar
You can ignore those files in your build.gradle:
android {
packagingOptions {
exclude 'META-INF/LICENSE'
}
}
Error:Execution failed for task ':app:packageDebug'.
> Duplicate files copied in APK META-INF/LICENSE
File 1: C:\Users\asus1\AndroidStudioProjects\HttpGet\app\libs\httpmime-4.5.2.jar
File 2: C:\Users\asus1\AndroidStudioProjects\HttpGet\app\libs\httpclient-win-4.5.2.jar
Information:BUILD FAILED
Information:Total time: 6.223 secs
Information:2 errors
Information:0 warnings
Information:See complete output in console
就在build.gradle里面加上
packagingOptions {
exclude 'META-INF/LICENSE'
}
再编译的时候如果还有同样的错误就再加,一直到没有为止