I'm writing an application with a model object that will expose a Restful interface to some web services. I've noticed that in Android there is a java.net.URI and an android.net.URI class. What are the benefits to using one versus the other? Has anyone else run into this and found that one works better than the other?
In the below code I'm parsing the individual parts of the URI into a java.net URI object so I can then call httpGet(URI uri)
. However, would there be any performance benefits or any benefits at all to using the android.net classes or just calling 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
...
}
In general, an URI (Uniform Resource Identifier) represents a resource. The URI class of Java represents this format You can get the URI format of a file by invoking the toURI() method.
A URI is a uniform resource identifier while a URL is a uniform resource locator.
InetAddress class is Java's encapsulation of an IP address. It is used by most of the other networking classes, including Socket , ServerSocket , URL , DatagramSocket , DatagramPacket , and more. This class represents an Internet address as two fields: hostName (a String ) and address (an int ).
Uniform Resource Identifier (URI) − a sequence of characters that allows the complete identification of any abstract or physical resource.
Yes, there will be performance benefits. The android team doesn't have to conform to the same backwards compatibility restrictions when coding the android.net
package that they do when they're implementing the java.net
package. Therefore they can make better optimizations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With