http-request 是 Android 网络框架,在小型的项目上性能表现最好。
使用:
The http-request library is available from Maven Central.
<dependency> <groupId>com.github.kevinsawicki</groupId> <artifactId>http-request</artifactId> <version>6.0</version> </dependency>
简单的AsyncTask异步加载示例:
private class DownloadTask extends AsyncTask<String, Long, File> { protected File doInBackground(String... urls) { try { HttpRequest request = HttpRequest.get(urls[0]); File file = null; if (request.ok()) { file = File.createTempFile("download", ".tmp"); request.receive(file); publishProgress(file.length()); } return file; } catch (HttpRequestException exception) { return null; } } protected void onProgressUpdate(Long... progress) { Log.d("MyApp", "Downloaded bytes: " + progress[0]); } protected void onPostExecute(File file) { if (file != null) Log.d("MyApp", "Downloaded file to: " + file.getAbsolutePath()); else Log.d("MyApp", "Download failed"); } } new DownloadTask().execute("http://google.com");