android.net 与 java.net 以及不同的 URI 类

标签 android

我正在编写一个带有模型对象的应用程序,它将向某些 Web 服务公开 Restful 接口(interface)。我注意到在 Android 中有一个 java.net.URI 和一个 android.net.URI 类。使用一个与另一个相比有什么好处?有没有其他人遇到过这个并发现一个比另一个更好?

在下面的代码中,我将 URI 的各个部分解析为一个 java.net URI 对象,这样我就可以调用 httpGet(URI uri)。但是,使用 android.net 类或仅调用 httpGet(String url) 是否有任何性能优势或任何优势?

public class RestMethods {
    private String protocol;
    private String host;
    private Integer port;
    private URI uri;

    public String restGet(String path) throws MalformedURLException, InterruptedException, ExecutionException{
        StringBuilder builder = new StringBuilder();
        try {
            // Execute HTTP Post Request
            HttpClient httpclient = new DefaultHttpClient();
            HttpGet httpget = new HttpGet(uri);
            HttpResponse response = httpclient.execute(httpget);
            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
            for (String line = null; (line = reader.readLine()) != null;) {
                builder.append(line).append("\n");
            }
        } catch (ClientProtocolException e) {
            return "Client Protocol Exception Exception " + e.toString();
        } catch (IOException e) {
            return "IO Exception " + e.toString();
        }   
        return builder.toString();
    }
...
//Other rest methods, Getters, and setters down here
...
}

最佳答案

是的,会有性能优势。 Android 团队在编写 android.net 包时不必遵守与实现 java.net 包时相同的向后兼容性限制。因此他们可以做出更好的优化。

关于android.net 与 java.net 以及不同的 URI 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7971275/

相关文章:

ListView 的 Android 布局,屏幕底部跟随非滚动文本栏

android - 我的 Sprite 如何从 andEngine 中的当前位置旋转(平滑旋转)给定角度?

javascript - 如何使用 Dropzonejs 将 Android 画廊中的照片上传到网站?

android - 场景背景不显示相机捕获

android - 我可以跟踪哪些关键字引导用户在 Google Play 上找到我的应用程序吗?

Android:为不同的第三方应用程序启动 Activity

java - 访问 Fragment 内的适配器

android - Android中两个ListView的同步滚动

android - 隐藏和取消隐藏 EditText

java - 使用 mp4parser 剪辑 mp4。无法通过 SyncSample 纠正过去的时间