一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

Android Post Get(最新 HttpClient 事例)

 擊碎星辰 2010-05-20

Android Post Get 示例 收藏
package alex.reader.ebook.bam;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;

public class SimpleClient extends Activity {

    private HttpParams httpParams;

    private HttpClient httpClient;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.simple_client);

        EditText editText = (EditText) this.findViewById(R.id.EditText01);

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("email", "firewings.r@gmail.com"));
        params.add(new BasicNameValuePair("password", "954619"));
        params.add(new BasicNameValuePair("remember", "1"));
        params.add(new BasicNameValuePair("from", "kx"));
        params.add(new BasicNameValuePair("login", "登 錄"));
        params.add(new BasicNameValuePair("refcode", ""));
        params.add(new BasicNameValuePair("refuid", "0"));

        Map params2 = new HashMap();

        params2.put("hl", "zh-CN");

        params2.put("source", "hp");

        params2.put("q", "haha");

        params2.put("aq", "f");

        params2.put("aqi", "g10");

        params2.put("aql", "");

        params2.put("oq", "");

        String url2 = "

        String url = "

        getHttpClient();

        editText.setText(doPost(url, params));

        // editText.setText(doGet(url2, params2));

    }

    public String doGet(String url, Map params) {

        /* 建立HTTPGet對象 */

        String paramStr = "";

        Iterator iter = params.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            Object key = entry.getKey();
            Object val = entry.getValue();
            paramStr += paramStr = "&" + key + "=" + val;
        }

        if (!paramStr.equals("")) {
            paramStr = paramStr.replaceFirst("&", "?");
            url += paramStr;
        }
        HttpGet httpRequest = new HttpGet(url);

        String strResult = "doGetError";

        try {

            /* 發(fā)送請求并等待響應(yīng) */
            HttpResponse httpResponse = httpClient.execute(httpRequest);
            /* 若狀態(tài)碼為200 ok */
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                /* 讀返回?cái)?shù)據(jù) */
                strResult = EntityUtils.toString(httpResponse.getEntity());

            } else {
                strResult = "Error Response: "
                        + httpResponse.getStatusLine().toString();
            }
        } catch (ClientProtocolException e) {
            strResult = e.getMessage().toString();
            e.printStackTrace();
        } catch (IOException e) {
            strResult = e.getMessage().toString();
            e.printStackTrace();
        } catch (Exception e) {
            strResult = e.getMessage().toString();
            e.printStackTrace();
        }

        Log.v("strResult", strResult);

        return strResult;
    }

    public String doPost(String url, List<NameValuePair> params) {

        /* 建立HTTPPost對象 */
        HttpPost httpRequest = new HttpPost(url);

        String strResult = "doPostError";

        try {
            /* 添加請求參數(shù)到請求對象 */
            httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
            /* 發(fā)送請求并等待響應(yīng) */
            HttpResponse httpResponse = httpClient.execute(httpRequest);
            /* 若狀態(tài)碼為200 ok */
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                /* 讀返回?cái)?shù)據(jù) */
                strResult = EntityUtils.toString(httpResponse.getEntity());

            } else {
                strResult = "Error Response: "
                        + httpResponse.getStatusLine().toString();
            }
        } catch (ClientProtocolException e) {
            strResult = e.getMessage().toString();
            e.printStackTrace();
        } catch (IOException e) {
            strResult = e.getMessage().toString();
            e.printStackTrace();
        } catch (Exception e) {
            strResult = e.getMessage().toString();
            e.printStackTrace();
        }

        Log.v("strResult", strResult);

        return strResult;
    }

    public HttpClient getHttpClient() {

        // 創(chuàng)建 HttpParams 以用來設(shè)置 HTTP 參數(shù)(這一部分不是必需的)

        this.httpParams = new BasicHttpParams();

        // 設(shè)置連接超時和 Socket 超時,以及 Socket 緩存大小

        HttpConnectionParams.setConnectionTimeout(httpParams, 20 * 1000);

        HttpConnectionParams.setSoTimeout(httpParams, 20 * 1000);

        HttpConnectionParams.setSocketBufferSize(httpParams, 8192);

        // 設(shè)置重定向,缺省為 true

        HttpClientParams.setRedirecting(httpParams, true);

        // 設(shè)置 user agent

        String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2) Gecko/20100115 Firefox/3.6";
        HttpProtocolParams.setUserAgent(httpParams, userAgent);

        // 創(chuàng)建一個 HttpClient 實(shí)例

        // 注意 HttpClient httpClient = new HttpClient(); 是Commons HttpClient

        // 中的用法,在 Android 1.5 中我們需要使用 Apache 的缺省實(shí)現(xiàn) DefaultHttpClient

        httpClient = new DefaultHttpClient(httpParams);

        return httpClient;
    }
}

 

本文來自CSDN博客,轉(zhuǎn)載請標(biāo)明出處:http://blog.csdn.net/firewings_r/archive/2010/03/12/5374851.aspx

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多

    欧美午夜一级特黄大片| 国产亚洲精品久久久优势| 久久亚洲精品中文字幕| 国产免费人成视频尤物| 亚洲一区二区三区中文久久| 热情的邻居在线中文字幕| 国产成人精品午夜福利| 欧美日韩国产精品黄片| 国产精品亚洲一区二区| 久久人妻人人澡人人妻| 欧美一级内射一色桃子| 观看日韩精品在线视频| 国产成人午夜福利片片| 成人午夜视频精品一区| 麻豆视传媒短视频免费观看 | 中日韩免费一区二区三区| 中文字幕av诱惑一区二区| 很黄很污在线免费观看| 精品香蕉一区二区在线| 嫩草国产福利视频一区二区| 国产av精品高清一区二区三区 | 欧美人妻盗摄日韩偷拍| 日本久久精品在线观看| 欧美黄色成人真人视频| 国产女优视频一区二区| 男女午夜视频在线观看免费| 日韩夫妻午夜性生活视频| 亚洲男人的天堂色偷偷| 国产原创中文av在线播放| 在线观看中文字幕91| 免费一级欧美大片免费看| 黄色激情视频中文字幕| 成年午夜在线免费视频| 日韩成人动画在线观看| 日本一品道在线免费观看| 日本视频在线观看不卡| 有坂深雪中文字幕亚洲中文 | 国产日韩久久精品一区| 成人亚洲国产精品一区不卡| 日韩成人动画在线观看| 麻豆果冻传媒一二三区|