中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

android的 Http工具類

2018-07-20    來源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬Linux鏡像隨意使用
android的HttpClient實(shí)現(xiàn)簡(jiǎn)單的get和post請(qǐng)求
/**
 * Http工具類
 */
public class HttpUtil {
    // 創(chuàng)建HttpClient對(duì)象
    public static HttpClient httpClient = new DefaultHttpClient();
    public static final String BASE_URL = "";
 
    /**
     * get請(qǐng)求
     *
     * @param url
     *            發(fā)送請(qǐng)求的URL
     * @return 服務(wù)器響應(yīng)字符串
     * @throws Exception
     */
    public static String doGet(String url) throws Exception {
        // 創(chuàng)建HttpGet對(duì)象。
        HttpGet get = new HttpGet(url);
        // 發(fā)送GET請(qǐng)求
        HttpResponse httpResponse = httpClient.execute(get);
        // 如果服務(wù)器成功地返回響應(yīng)
        if (httpResponse.getStatusLine().getStatusCode() == 200) {
            // 獲取服務(wù)器響應(yīng)字符串
            HttpEntity entity = httpResponse.getEntity();
            InputStream content = entity.getContent();
            return convertStreamToString(content);
        }
        return null;
    }
 
    /**
     * post請(qǐng)求
     *
     * @param url
     *            發(fā)送請(qǐng)求的URL
     * @param params
     *            請(qǐng)求參數(shù)
     * @return 服務(wù)器響應(yīng)字符串
     * @throws Exception
     */
    public static String doPost(String url, Map<String, String> rawParams)
            throws Exception {
        // 創(chuàng)建HttpPost對(duì)象。
        HttpPost post = new HttpPost(url);
        // 如果傳遞參數(shù)個(gè)數(shù)比較多的話可以對(duì)傳遞的參數(shù)進(jìn)行封裝
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        for (String key : rawParams.keySet()) {
            // 封裝請(qǐng)求參數(shù)
            params.add(new BasicNameValuePair(key, rawParams.get(key)));
        }
        // 設(shè)置請(qǐng)求參數(shù)
        post.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
        // 發(fā)送POST請(qǐng)求
        HttpResponse httpResponse = httpClient.execute(post);
        // 如果服務(wù)器成功地返回響應(yīng)
        if (httpResponse.getStatusLine().getStatusCode() == 200) {
            // 獲取服務(wù)器響應(yīng)字符串
            HttpEntity entity = httpResponse.getEntity();
            InputStream content = entity.getContent();
            return convertStreamToString(content);
        }
        return null;
    }
 
    /**
     * 獲取服務(wù)器的響應(yīng),轉(zhuǎn)換為字符串
     */
    private static String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}

標(biāo)簽: 服務(wù)器

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:iOS晃動(dòng)檢測(cè)

下一篇:PHP縮略圖類