Android中使用HTTP和HttpClient进行通信

/**
     * 使用HTTP的Get方式进行数据请求
     */
    protected void httpGet() {
        /**
         * 进行异步请求
         */
        new AsyncTask<String, Void, Void>() {

            @Override
            protected Void doInBackground(String... params) {
                System.err.println("httpGet start");
                // 在此方法中只能进行数据处理,不能与进行UI交互
                try {
                    URL url = new URL(params[0]);
                    URLConnection connection = url.openConnection();
                    InputStream is = connection.getInputStream();
                    // 使用UTF-8的方式进行数据流转化,从字节流转化为字符流
                    InputStreamReader isr = new InputStreamReader(is, "UTF-8");
                    BufferedReader br = new BufferedReader(isr);
                    String line = null;
                    while ((line = br.readLine()) != null) {
                        System.err.println(line);
                    }
                    // 关闭数据流
                    br.close();
                    isr.close();
                    is.close();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.execute("http://fanyi.youdao.com/openapi.do?keyfrom=***&key=***&type=data&doctype=json&version=1.1&q=good");
    }

    /**
     * 使用HTTP的Post方式进行数据请求
     */
    protected void httpPost() {
        /**
         * 进行异步请求
         */
        new AsyncTask<String, Void, Void>() {

            @Override
            protected Void doInBackground(String... params) {
                System.err.println("httpPost start");
                // 在此方法中只能进行数据处理,不能与进行UI交互
                try {
                    URL url = new URL(params[0]);
                    HttpURLConnection connection = (HttpURLConnection) url
                            .openConnection();
                    // 设置请求方式以及设置参数

                    // 设置是否向HttpURLConnection对象输出
                    connection.setDoOutput(true);
                    // 设置请求方式
                    connection.setRequestMethod("POST");

                    // 设置参数
                    OutputStream os = connection.getOutputStream();
                    OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
                    BufferedWriter bw = new BufferedWriter(osw);
                    bw.write(params[1]);
                    bw.flush();

                    InputStream is = connection.getInputStream();
                    // 使用UTF-8的方式进行数据流转化,从字节流转化为字符流
                    InputStreamReader isr = new InputStreamReader(is, "UTF-8");
                    BufferedReader br = new BufferedReader(isr);
                    String line = null;
                    while ((line = br.readLine()) != null) {
                        System.err.println(line);
                    }
                    // 关闭数据流
                    br.close();
                    isr.close();
                    is.close();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.execute("http://fanyi.youdao.com/openapi.do",
                "keyfrom=***&key=***&type=data&doctype=json&version=1.1&q=good");

    }

    private HttpClient httpClient;

    /**
     * 使用HttpClient进行Get请求
     */
    protected void httpClientGet() {
        new AsyncTask<String, Void, Void>() {

            @Override
            protected Void doInBackground(String... params) {
                System.err.println("httpClientGet start");
                String urlString = params[0];
                HttpGet httpGet = new HttpGet(urlString);
                try {
                    // 发送请求
                    HttpResponse response = httpClient.execute(httpGet);
                    // 获取返回内容
                    String value = EntityUtils.toString(response.getEntity());
                    System.err.println(value);
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.execute("http://fanyi.youdao.com/openapi.do?keyfrom=***&key=***&type=data&doctype=json&version=1.1&q=good");
    }

    /**
     * 使用HttpClient进行Post请求
     */
    protected void httpClientPost() {
        new AsyncTask<String, Void, Void>() {

            @Override
            protected Void doInBackground(String... params) {
                System.err.println("httpClientPost start");
                String urlString = params[0];
                HttpPost httpPost = new HttpPost(urlString);
                try {
                    // 设置参数
                    String[] paramsArr = params[1].split("&");
                    int len = paramsArr.length;
                    List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>(
                            len);
                    BasicNameValuePair pair = null;
                    for (int i = 0; i < len; i++) {
                        String[] paramArr = paramsArr[i].split("=");
                        pair = new BasicNameValuePair(paramArr[0], paramArr[1]);
                        list.add(pair);
                    }
                    httpPost.setEntity(new UrlEncodedFormEntity(list));
                } catch (UnsupportedEncodingException e1) {
                    e1.printStackTrace();
                }

                try {
                    // 发送请求
                    HttpResponse response = httpClient.execute(httpPost);
                    // 获取返回内容
                    String value = EntityUtils.toString(response.getEntity());
                    System.err.println(value);
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.execute("http://fanyi.youdao.com/openapi.do",
                "keyfrom=***&key=***&type=data&doctype=json&version=1.1&q=good");
    }

截图:

 

 

 

 

 

 

posted @ 2014-11-16 21:21  qingyezhu  阅读(1118)  评论(0编辑  收藏  举报