android.net vs java.net和不同的URI类

时间:2011-11-01 18:43:34

标签: android

我正在编写一个带有模型对象的应用程序,该模型对象会向某些Web服务公开Restful接口。我注意到在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
...
}

2 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:3)

看看this。这应该有所帮助。