android.net VS java.net和不同的URI类不同、net、android、VS

由网友(专属的偏见)分享简介:我正在写一个模型对象的应用程序将公开一个RESTful接口的一些Web服务。我注意到,在Android中有一个java.net.URI中和android.net.URI类。什么是使用一种与其他的好处?有没有其他人遇到了这一点,并发现其中一个比另一个好?在低于code我解析URI的各个部分成java.net URI对象,...

我正在写一个模型对象的应用程序将公开一个RESTful接口的一些Web服务。我注意到,在Android中有一个java.net.URI中和android.net.URI类。什么是使用一种与其他的好处?有没有其他人遇到了这一点,并发现其中一个比另一个好?

在低于code我解析URI的各个部分成java.net URI对象,这样我就可以调用 HTTPGET(URI URI) 。然而,会有在所有的任何性能优势,或任何利益使用android.net类或只调用HTTPGET(字符串URL)?

 公共类RestMethods {
    私人字符串协议;
    私人字符串主机;
    私人整数口;
    私人URI URI;

    公共字符串restGet(String path)方法抛出MalformedURLException的,InterruptedException的,为ExecutionException {
        StringBuilder的建设者=新的StringBuilder();
        尝试 {
            //执行HTTP POST请求
            HttpClient的HttpClient的=新DefaultHttpClient();
            HTTPGET HTTPGET =新HTTPGET(URI);
            HTT presponse响应= httpclient.execute(HTTPGET);
            的BufferedReader读卡器=新的BufferedReader(新的InputStreamReader(response.getEntity()的getContent(),UTF-8));
            对(串线= NULL;!(行= reader.readLine())= NULL){
                builder.append(线).append( N);
            }
        }赶上(ClientProtocolException E){
            回报客户端协议异常异常+ e.toString();
        }赶上(IOException异常E){
            返回IO异常+ e.toString();
        }
        返回builder.toString();
    }
...
//其他的休息方法,吸气剂,和setter这儿
...
}
 

解决方案

看看的这个。这将有助于。

在IOS和Android的严防死守下,APP已经很难获取到你的权限了

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

解决方案

Take a look at this. This should help.

阅读全文

相关推荐

最新文章