中文字幕 另类精品,亚洲欧美一区二区蜜桃,日本在线精品视频免费,孩交精品乱子片免费

<sup id="3hn2b"></sup>

    1. <sub id="3hn2b"><ol id="3hn2b"></ol></sub><legend id="3hn2b"></legend>

      1. <xmp id="3hn2b"></xmp>

      2. 新聞中心

        EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > Android網(wǎng)絡(luò)編程之Http通信

        Android網(wǎng)絡(luò)編程之Http通信

        作者: 時(shí)間:2016-09-12 來源:網(wǎng)絡(luò) 收藏

        Android中提供的HttpURLConnection和HttpClient接口可以用來開發(fā)HTTP程序。以下是本人在學(xué)習(xí)中的總結(jié)與歸納。

        本文引用地址:http://www.antipu.com.cn/article/201609/305041.htm

        1. HttpURLConnection接口

        首先需要明確的是,Http通信中的POST和GET請(qǐng)求方式的不同。GET可以獲得靜態(tài)頁面,也可以把參數(shù)放在URL字符串后面,傳遞給服務(wù)器。而POST方法的參數(shù)是放在Http請(qǐng)求中。因此,在編程之前,應(yīng)當(dāng)首先明確使用的請(qǐng)求方法,然后再根據(jù)所使用的方式選擇相應(yīng)的編程方式。

        HttpURLConnection是繼承于URLConnection類,二者都是抽象類。其對(duì)象主要通過URL的openConnection方法獲得。創(chuàng)建方法如下代碼所示:

        URL url = new URL(http://www.51cto.com/index.jsp?par=123456);

        HttpURLConnection urlConn=(HttpURLConnection)url.openConnection();

        過以下方法可以對(duì)請(qǐng)求的屬性進(jìn)行一些設(shè)置,如下所示:

        //設(shè)置輸入和輸出流

        urlConn.setDoOutput(true);

        urlConn.setDoInput(true);

        //設(shè)置請(qǐng)求方式為POST

        urlConn.setRequestMethod(POST);

        //POST請(qǐng)求不能使用緩存

        urlConn.setUseCaches(false);

        //關(guān)閉連接

        urlConn.disConnection();

        HttpURLConnection默認(rèn)使用GET方式,例如下面代碼所示:

        //使用HttpURLConnection打開連接

        HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();

        //得到讀取的內(nèi)容(流)

        InputStreamReader in = new InputStreamReader(urlConn.getInputStream());

        // 為輸出創(chuàng)建BufferedReader

        BufferedReader buffer = new BufferedReader(in);

        String inputLine = null;

        //使用循環(huán)來讀取獲得的數(shù)據(jù)

        while (((inputLine = buffer.readLine()) != null))

        {

        //我們?cè)诿恳恍泻竺婕由弦粋€(gè)n來換行

        resultData += inputLine + n;

        }

        //關(guān)閉InputStreamReader

        in.close();

        //關(guān)閉http連接

        urlConn.disconnect();

        如果需要使用POST方式,則需要setRequestMethod設(shè)置。代碼如下:

        String httpUrl = http://192.168.1.110:8080/httpget.jsp;

        //獲得的數(shù)據(jù)

        String resultData = ;

        URL url = null;

        try

        {

        //構(gòu)造一個(gè)URL對(duì)象

        url = new URL(httpUrl);

        }

        catch (MalformedURLException e)

        {

        Log.e(DEBUG_TAG, MalformedURLException);

        }

        if (url != null)

        {

        try

        {

        // 使用HttpURLConnection打開連接

        HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();

        //因?yàn)檫@個(gè)是post請(qǐng)求,設(shè)立需要設(shè)置為true

        urlConn.setDoOutput(true);

        urlConn.setDoInput(true);

        // 設(shè)置以POST方式

        urlConn.setRequestMethod(POST);

        // Post 請(qǐng)求不能使用緩存

        urlConn.setUseCaches(false);

        urlConn.setInstanceFollowRedirects(true);

        // 配置本次連接的Content-type,配置為application/x-www-form-urlencoded的

        urlConn.setRequestProperty(Content-Type,application/x-www-form-urlencoded);

        // 連接,從postUrl.openConnection()至此的配置必須要在connect之前完成,

        // 要注意的是connection.getOutputStream會(huì)隱含的進(jìn)行connect。

        urlConn.connect();

        //DataOutputStream流

        DataOutputStream out = new DataOutputStream(urlConn.getOutputStream());

        //要上傳的參數(shù)

        String content = par= + URLEncoder.encode(ABCDEFG, gb2312);

        //將要上傳的內(nèi)容寫入流中

        out.writeBytes(content);

        //刷新、關(guān)閉

        out.flush();

        out.close();

        2. HttpClient接口

        使用Apache提供的HttpClient接口同樣可以進(jìn)行HTTP操作。

        對(duì)于GET和POST請(qǐng)求方法的操作有所不同。GET方法的操作代碼示例如下:

        // http地址

        String httpUrl = http://192.168.1.110:8080/httpget.jsp?par=HttpClient_android_Get;

        //HttpGet連接對(duì)象

        HttpGet httpRequest = new HttpGet(httpUrl);

        //取得HttpClient對(duì)象

        HttpClient httpclient = new DefaultHttpClient();

        //請(qǐng)求HttpClient,取得HttpResponse

        HttpResponse httpResponse = httpclient.execute(httpRequest);

        //請(qǐng)求成功

        if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK)

        {

        //取得返回的字符串

        String strResult = EntityUtils.toString(httpResponse.getEntity());

        mTextView.setText(strResult);

        }

        else

        {

        mTextView.setText(請(qǐng)求錯(cuò)誤!);

        }

        }

        使用POST方法進(jìn)行參數(shù)傳遞時(shí),需要使用NameValuePair來保存要傳遞的參數(shù)。,另外,還需要設(shè)置所使用的字符集。代碼如下所示:

        // http地址

        String httpUrl = http://192.168.1.110:8080/httpget.jsp;

        //HttpPost連接對(duì)象

        HttpPost httpRequest = new HttpPost(httpUrl);

        //使用NameValuePair來保存要傳遞的Post參數(shù)

        List params = new ArrayList();

        //添加要傳遞的參數(shù)

        params.add(new BasicNameValuePair(par, HttpClient_android_Post));

        //設(shè)置字符集

        HttpEntity httpentity = new UrlEncodedFormEntity(params, gb2312);


        上一頁 1 2 下一頁

        關(guān)鍵詞:

        評(píng)論


        相關(guān)推薦

        技術(shù)專區(qū)

        關(guān)閉