当前位置: 移动技术网 > IT编程>移动开发>Android > Android通过HttpURLConnection和HttpClient接口实现网络编程

Android通过HttpURLConnection和HttpClient接口实现网络编程

2019年07月24日  | 移动技术网IT编程  | 我要评论

广州市委书记万庆良,好梦学车,虾米电影网

android中提供的httpurlconnection和httpclient接口可以用来开发http程序。以下是学习中的一些经验。

1、httpurlconnection接口

        首先需要明确的是,http通信中的post和get请求方式的不同。get可以获得静态页面,也可以把参数放在url字符串后面,传递给服务器。而post方法的参数是放在http请求中。因此,在编程之前,应当首先明确使用的请求方法,然后再根据所使用的方式选择相应的编程方式。httpurlconnection是继承于urlconnection类,二者都是抽象类。其对象主要通过url的openconnection方法获得。创建方法如下代码所示:

复制代码 代码如下:

url url = new url("http://www.xxx.com/index.jsp?type=231");
httpurlconnection urlconn=(httpurlconnection)url.openconnection();

通过以下方法可以对请求的属性进行一些设置,如下所示:

复制代码 代码如下:

//设置输入和输出流
urlconn.setdooutput(true);
urlconn.setdoinput(true);
//设置请求方式为post
urlconn.setrequestmethod("post");
//post请求不能使用缓
urlconn.setusecaches(false);
urlconn.disconnection();

httpurlconnection默认使用get方式,例如下面代码所示:

复制代码 代码如下:

httpurlconnection urlconn = (httpurlconnection) url.openconnection();
inputstreamreader in = new inputstreamreader(urlconn.getinputstream());
bufferedreader buffer = new bufferedreader(in);
string inputline = null;
while (((inputline = buffer.readline()) != null)) {
    resultdata += inputline + "\n";
}
in.close();
urlconn.disconnect();

如果需要使用post方式,则需要setrequestmethod设置。代码如下:

复制代码 代码如下:

string httpurl = "http://www.xxx.com/getuser.jsp";
string resultdata = "";
url url = null;
try {
    url = new url(httpurl);
} catch (malformedurlexception e) {
    log.e(debug_tag, "malformedurlexception");
}
if (url != null) { 
    try {
        httpurlconnection urlconn = (httpurlconnection) url.openconnection(); 
        //因为这个是post请求,设立需要设置为true 
        urlconn.setdooutput(true); 
        urlconn.setdoinput(true);
        urlconn.setrequestmethod("post"); // 设置以post方式  
        urlconn.setusecaches(false); 
        urlconn.setinstancefollowredirects(true);
        urlconn.setrequestproperty("content-type","application/x-www-form-urlencoded"); 
        // 这些配置必须要在connect之前完成, 
        // 要注意的是connection.getoutputstream会隐含的进行connect。 
        urlconn.connect(); 
        
        // dataoutputstream流
        dataoutputstream out = new dataoutputstream(urlconn.getoutputstream());
        string content = "name=" + urlencoder.encode("张三", "gb2312");   
        out.writebytes(content);  
        out.flush();
        out.close();
    } catch(exception e) {
        //
    }
}

2、httpclient接口

        使用apache提供的httpclient接口同样可以进行http操作。对于get和post请求方法的操作有所不同。get方法的操作代码示例如下:

复制代码 代码如下:

string httpurl = "http://www.xxx.com/getuser.jsp?par=123"; 
// httpget连接对象 
httpget httprequest = new httpget(httpurl);  
httpclient httpclient = new defaulthttpclient(); 
httpresponse httpresponse = httpclient.execute(httprequest); 
// 请求成功 
if (httpresponse.getstatusline().getstatuscode() == httpstatus.sc_ok) { 
    // 取得返回的字符串 
    string strresult = entityutils.tostring(httpresponse.getentity()); 
    mtextview.settext(strresult); 
} else { 
    mtextview.settext("请求错误!"); 
}

使用post方法进行参数传递时,需要使用namevaluepair来保存要传递的参数。另外,还需要设置所使用的字符集。代码如下所示:

复制代码 代码如下:

string httpurl = "http://www.xxx.com/getuser.jsp";   
httppost httprequest = new httppost(httpurl);
list<namevaluepair> params = new arraylist<namevaluepair>();   
params.add(new basicnamevaluepair("userid", "123")); 
httpentity httpentity = new urlencodedformentity(params, "gb2312"); //设置字符集
httprequest.setentity(httpentity);
//取得默认的httpclient 
httpclient httpclient = new defaulthttpclient(); 
//取得httpresponse 
httpresponse httpresponse = httpclient.execute(httprequest); 
//httpstatus.sc_ok表示连接成功 
if (httpresponse.getstatusline().getstatuscode() == httpstatus.sc_ok) { 
    // 取得返回的字符串 
    string strresult = entityutils.tostring(httpresponse.getentity()); 
    mtextview.settext(strresult); 
} else { 
    mtextview.settext("请求错误!"); 
}

        httpclient实际上是对java提供方法的一些封装,在httpurlconnection中的输入输出流操作,在这个接口中被统一封装成了httppost(httpget)和httpresponse,这样,就减少了操作的繁琐性。

以上所述就是本文的全部内容了,希望大家能够喜欢。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网