我正在编写一个带有模型对象的应用程序,该对象将向某些 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
...
}
是的,会有性能优势。 Android 团队在编码时不必遵守相同的向后兼容性限制android.net
他们在实施时所做的包java.net
包裹。因此他们可以做出更好的优化。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)