Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android.net vs java.net and the different URI classes

Tags:

android

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
...
}
like image 641
LowDev1 Avatar asked Nov 01 '11 18:11

LowDev1


People also ask

Does Java have a URI class?

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.

What is the URI in Android?

A URI is a uniform resource identifier while a URL is a uniform resource locator.

What does the Java net URI address class represent?

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 ).

What is URI in spring boot?

Uniform Resource Identifier (URI) − a sequence of characters that allows the complete identification of any abstract or physical resource.


1 Answers

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.

like image 122
Kurtis Nusbaum Avatar answered Nov 01 '22 14:11

Kurtis Nusbaum