我正在编写一个带有模型对象的应用程序,它将向某些 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/