HTTP基础与Android之(安卓与服务器通信)——使用HttpClient和HttpURLConnection

查看原文:http://blog.csdn.net/sinat_29912455/article/details/51122286

1、客户端连接服务器实现内部的原理:

这里写图片描述

分析上图,步骤如下:

第一步:在浏览器客户端中得到用户输入的内容。

第二步:浏览器得到这个网址之后,内部会将这个域名发送到DNS上,进行域名解析。得到它的IP之后就会链接到指定的服务器上,假如服务器的地址是:221.104.13.32:80,从浏览器到服务器端口它使用到最底层的TCP/IP协议。

第三步:实现TCP/IP协议用Socket来完成,使用了Socket的套接字。

第四步:服务器端的80端口监听客户端的链接,这样客户端到服务器就链接上了。
服务器接收到这些内容之后,并按照这些请求的路径找到对应的页面,进一步找到对应的网页内容,返回给客户端。

通俗一点讲,用户在浏览器输入网址,通过http协议发出去,网址经过DNS域名解析,解析成指定的ip地址,并在80端口上监听用户的请求。服务器监听到请求之后,会以三种方式返回给客户端:HTML、XML、JASON。

GET方式和POST方式的差别:

GET是从服务器上获取数据,POST是向服务器传送数据。
在客户端,GET方式在通过URL提交数据,数据在URL中可以看到;POST方式,数据放在HTML HEADER内提交。
对于GET方式,服务器端用Request.QueryString获取变量的值,对于POST方式,服务器用Request.Form获取提交的数据。
GET方式提交的数据不能大于2KB(主要是URL长度限制),而POST则没有此限制。
安全性问题。正如2中提到,使用GET的时候,参数会显示在地址栏上,而POST不会。所以,如果这些数据是中文数据而且是非敏感数据,那么使用GET;如果用户输入的数据不是中文字符而且包含敏感数据,那么还是使用POST为好。

HTTP返回请求数据的三种方式:

1、以HTML代码内容返回。

2、以XML字符串的形式返回,在以后的android开发中这种形式返回数据比较多。

3、以JSON对象形式返回,在网络流量上考虑JSON要比XML方式要好一些,便于解析。

在Android当中,一般使用xml和Json数据解析。

2、使用HTTP协议访问网络:

Android中的WebView控件已经在后台帮我们处理好了发送HTTP请求、接收服务响应、解析返回数据,以及最终的页面展示这几步工作,不 过由于它封装得太好了,反而不能直观地看出HTTP协议是如何工作的。因此接下来我们通过手动发送HTTP请求的方式,来更加深入的了解这一过程。

在Android上发送HTTP请求的方式一般有两种:HttpURLConnection和HttpCient。我们先来学习HttpCient。

3、HttpCient:

HttpClient是Apache开源组织提供的HTTP网络访问接口(一个开源的项目),从名字上就可以看出,它是一个简单的HTTP客户端 (并不是浏览器),可以发送HTTP请求,接受HTTP响应。但是不会缓存服务器的响应,不能执行HTTP页面中签入嵌入的JS代码,自然也不会对页面内 容进行任何解析、处理,这些都是需要开发人员来完成的。

现在Android已经成功集成了HttpClient,所以开发人员在Android项目中可以直接使用HttpClient来想Web站点提交 请求以及接受响应,如果使用其他的Java项目,需要引入进相应的Jar包。HttpClient可以在官网上下载。官网链接:http://hc.apache.org/downloads.cgi

HttpClient其实是一个interface类型,HttpClient封装了对象需要执行的Http请求、身份验证、连接管理和其它特性。既然HttpClient是一个接口,因此无法创建它的实例。从文档上看,HttpClient有三个已知的实现类分别是:AbstractHttpClient, AndroidHttpClient, DefaultHttpClient,会发现有一个专门为Android应用准备的实现类AndroidHttpClient,当然使用常规的DefaultHttpClient也可以实现功能。

从两个类包所有在位置就可以看出区别,AndroidHttpClient定义在 android.net.http.AndroidHttpClient包下,属于Android原生的http访问,而 DefaultHttpClient定义在org.apache.http.impl.client.DefaultHttpClient包下,属于对 apche项目的支持。而AndroidHttpClient没有公开的构造函数,只能通过静态方法newInstance()方法来获得 AndroidHttpClient对象。

简单来说,用HttpClient发送请求、接收响应都很简单,只需要五大步骤即可:(要牢记)

1、创建代表客户端的HttpClient对象。

2、创建代表请求的对象,如果需要发送GET请求,则创建HttpGet对象,如果需要发送POST请求,则创建HttpPost对象。注:对于发 送请求的参数,GET和POST使用的方式不同,GET方式可以使用拼接字符串的方式,把参数拼接在URL结尾;POST方式需要使用 setEntity(HttpEntity entity)方法来设置请求参数。

3、调用HttpClient对象的execute(HttpUriRequest request)发送请求,执行该方法后,将获得服务器返回的HttpResponse对象。服务器发还给我们的数据就在这个HttpResponse相 应当中。调用HttpResponse的对应方法获取服务器的响应头、响应内容等。

4、检查相应状态是否正常。服务器发给客户端的相应,有一个相应码:相应码为200,正常;相应码为404,客户端错误;相应码为505,服务器端错误。

5、获得相应对象当中的数据

4、DefaultHttpClient:

GET方式

布局文件,activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Send Request" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/TextView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
    </ScrollView>

</LinearLayout>

布局文件中,我们用一个ScrollView来包裹TextView。借助ScrollView控件的话,就可以允许我们一滚动的形式查看屏幕外i的那部分内容。

MainActivity.java的代码如下:

package com.example.m04_http01;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    public static final int SHOW_RESPONSE = 0;

    private Button button_sendRequest;
    private TextView textView_response;

    //新建Handler的对象,在这里接收Message,然后更新TextView控件的内容
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
            case SHOW_RESPONSE:
                String response = (String) msg.obj;
                textView_response.setText(response);
                break;

            default:
                break;
            }            
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView_response = (TextView)findViewById(R.id.TextView1);
        button_sendRequest = (Button)findViewById(R.id.button1);

        button_sendRequest.setOnClickListener(new OnClickListener() {

            //点击按钮时,执行sendRequestWithHttpClient()方法里面的线程
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                sendRequestWithHttpClient();
            }
        });
    }

    //方法:发送网络请求,获取百度首页的数据。在里面开启线程
    private void sendRequestWithHttpClient() {
        new Thread(new Runnable() {

            @Override
            public void run() {
                //用HttpClient发送请求,分为五步
                //第一步:创建HttpClient对象
                HttpClient httpCient = new DefaultHttpClient();
                //第二步:创建代表请求的对象,参数是访问的服务器地址
                HttpGet httpGet = new HttpGet("http://www.baidu.com");

                try {
                    //第三步:执行请求,获取服务器发还的相应对象
                    HttpResponse httpResponse = httpCient.execute(httpGet);
                    //第四步:检查相应的状态是否正常:检查状态码的值是200表示正常
                    if (httpResponse.getStatusLine().getStatusCode() == 200) {
                        //第五步:从相应对象当中取出数据,放到entity当中
                        HttpEntity entity = httpResponse.getEntity();
                        String response = EntityUtils.toString(entity,"utf-8");//将entity当中的数据转换为字符串

                        //在子线程中将Message对象发出去
                        Message message = new Message();
                        message.what = SHOW_RESPONSE;
                        message.obj = response.toString();
                        handler.sendMessage(message);
                    }

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }).start();//这个start()方法不要忘记了        

    }    

最后要记得在清单文件中声明访问网络的权限:

<uses-sdk

android:minSdkVersion="8"

android:targetSdkVersion="16" />

<uses-permission android:name="android.permission.INTERNET"/>

这里写图片描述

POST方式

POST提交数据的步骤:

  1. 构造请求对象;
  2. 将需要传递给服务器端的数据放置在键值对对象当中;
  3. 将准备好的键值对放置在List当中;
  4. 生成代表请求体的对象;
  5. 将存有请求键值对的List对象放置在请求题对象当中;
  6. 将请求体对象放置到请求对象当中;
  7. 发送请求对象
    后面的步骤(即处理请求对象)和GET方法是一致的。
package com.example.m04_http02;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

    private EditText nameText;
    private EditText pwdText;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        nameText = (EditText) findViewById(R.id.nameText);
        pwdText = (EditText) findViewById(R.id.pwdText);
        button = (Button) findViewById(R.id.button1);

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // 用户输入用户名密码, 然后通过Get方法发送给本地服务器
                String name = nameText.getText().toString();
                String pwd = pwdText.getText().toString();

                // 使用GET方法向本地服务器发送数据
                //GetThread getThread = new GetThread(name, pwd);
                //getThread.start();                

                //使用POST方法向服务器发送数据
                PostThread postThread = new PostThread(name, pwd);
                postThread.start();
            }
        });
    }


    //子线程:通过GET方法向服务器发送用户名、密码的信息
    class GetThread extends Thread {

        String name;
        String pwd;

        public GetThread(String name, String pwd) {
            this.name = name;
            this.pwd = pwd;
        }

        @Override
        public void run() {
            //用HttpClient发送请求,分为五步
            //第一步:创建HttpClient对象
            HttpClient httpClient = new DefaultHttpClient();
            //注意,下面这一行中,我之前把链接中的"test"误写成了"text",导致调BUG调了半天没弄出来,真是浪费时间啊
            String url = "http://192.168.1.112:8080/test.jsp?name=" + name+ "&password=" + pwd;
            //第二步:创建代表请求的对象,参数是访问的服务器地址
            HttpGet httpGet = new HttpGet(url);
            try {
                //第三步:执行请求,获取服务器发还的相应对象
                HttpResponse response = httpClient.execute(httpGet);
                //第四步:检查相应的状态是否正常:检查状态码的值是200表示正常
                if (response.getStatusLine().getStatusCode() == 200) {
                    //第五步:从相应对象当中取出数据,放到entity当中
                    HttpEntity entity = response.getEntity();
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(entity.getContent()));
                    String result = reader.readLine();
                    Log.d("HTTP", "GET:" + result);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }    

    //子线程:使用POST方法向服务器发送用户名、密码等数据
    class PostThread extends Thread {

        String name;
        String pwd;

        public PostThread(String name, String pwd) {
            this.name = name;
            this.pwd = pwd;
        }

        @Override
        public void run() {
            HttpClient httpClient = new DefaultHttpClient();
            String url = "http://192.168.1.112:8080/test.jsp";
            //第二步:生成使用POST方法的请求对象
            HttpPost httpPost = new HttpPost(url);
            //NameValuePair对象代表了一个需要发往服务器的键值对
            NameValuePair pair1 = new BasicNameValuePair("name", name);
            NameValuePair pair2 = new BasicNameValuePair("password", pwd);
            //将准备好的键值对对象放置在一个List当中
            ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();
            pairs.add(pair1);
            pairs.add(pair2);
            try {
                //创建代表请求体的对象(注意,是请求体)
                HttpEntity requestEntity = new UrlEncodedFormEntity(pairs);
                //将请求体放置在请求对象当中
                httpPost.setEntity(requestEntity);
                //执行请求对象
                try {
                    //第三步:执行请求对象,获取服务器发还的相应对象
                    HttpResponse response = httpClient.execute(httpPost);
                    //第四步:检查相应的状态是否正常:检查状态码的值是200表示正常
                    if (response.getStatusLine().getStatusCode() == 200) {
                        //第五步:从相应对象当中取出数据,放到entity当中
                        HttpEntity entity = response.getEntity();
                        BufferedReader reader = new BufferedReader(
                                new InputStreamReader(entity.getContent()));
                        String result = reader.readLine();
                        Log.d("HTTP", "POST:" + result);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}

这里写图片描述

5、Java中使用HTTP——HttpURLConnection

HttpURLConnection继承了URLConnection,所以在URLConnection的基础上进一步改进,增加了一些用于操作 HTTP资源的便捷方法。Java中HttpURLConnection对象通过URL.openConnection()方法来获得,需要进行强制转 换。先来介绍几个HttpURLConnection的常用方法:

  • void setConnectTimeout(int timeout):设置连接超时时长,如果超过timeout时长,则放弃连接,单位以毫秒计算。

  • void setDoInput(boolean newValue) :标志是否允许输入。

  • void setDoOutput(boolean newValue):标志是否允许输出。

  • String getRequestMethod():获取发送请求的方法。

  • int getResponseCode():获取服务器的响应码。

  • void setRequestMethod(String method):设置发送请求的方法。

  • void setRequestProperty(String field,String newValue):设置请求报文头,并且只对当前HttpURLConnection有效。

GET方式

这个例子通过GET方式从服务端获取一张图片的信息,并把其保存在本地磁盘中。服务器为本机上的IIS,一张静态图片,直接通过URL访问。

package com.http.get;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class HttpUtils {
    private static String URL_PATH = "http://192.168.1.106:8080/green.jpg";
    /**
     * @param args
     */
    public static void main(String[] args) {
        // 调用方法获取图片并保存
        saveImageToDisk();
    }
    /**
     * 通过URL_PATH的地址访问图片并保存到本地
     */
    public static void saveImageToDisk()
    {
        InputStream inputStream= getInputStream();
        byte[] data=new byte[1024];
        int len=0;
        FileOutputStream fileOutputStream=null;
        try {
            //把图片文件保存在本地F盘下
            fileOutputStream=new FileOutputStream("F:\test.png");
            while((len=inputStream.read(data))!=-1) 
            {
                //向本地文件中写入图片流
                fileOutputStream.write(data,0,len);                
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally
        {
            //最后关闭流
            if(inputStream!=null)
            {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fileOutputStream!=null)
            {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    /**
     * 通过URL获取图片
     * @return URL地址图片的输入流。
     */
    public static InputStream getInputStream() {
        InputStream inputStream = null;
        HttpURLConnection httpURLConnection = null;

        try {
            //根据URL地址实例化一个URL对象,用于创建HttpURLConnection对象。
            URL url = new URL(URL_PATH);

            if (url != null) {
                //openConnection获得当前URL的连接
                httpURLConnection = (HttpURLConnection) url.openConnection();
                //设置3秒的响应超时
                httpURLConnection.setConnectTimeout(3000);
                //设置允许输入
                httpURLConnection.setDoInput(true);
                //设置为GET方式请求数据
                httpURLConnection.setRequestMethod("GET");
                //获取连接响应码,200为成功,如果为其他,均表示有问题
                int responseCode=httpURLConnection.getResponseCode();
                if(responseCode==200)
                {
                    //getInputStream获取服务端返回的数据流。
                    inputStream=httpURLConnection.getInputStream();
                }
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return inputStream;
    }

}

POST方式

 这个例子通过POST方式访问一个登陆页面,需要输入用户名(username)和密码(password)。虽然这里使用的Java在讲解问 题,但是服务端是使用.Net的框架,一个很简单的HTML页面加一个表单传送的一般处理程序,输入为admin+123为登陆成功,这里不累述了。
 

package com.http.post;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class postUtils {

    private static String PATH = "http://192.168.222.1:1231/loginas.ashx";
    private static URL url;

    public postUtils() {
    }
    static {
        try {
            url = new URL(PATH);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 通过给定的请求参数和编码格式,获取服务器返回的数据
     * @param params 请求参数
     * @param encode 编码格式
     * @return 获得的字符串
     */
    public static String sendPostMessage(Map<String, String> params,
            String encode) {
        StringBuffer buffer = new StringBuffer();
        if (params != null && !params.isEmpty()) {
            for (Map.Entry<String, String> entry : params.entrySet()) {
                try {
                    buffer.append(entry.getKey())
                            .append("=")
                            .append(URLEncoder.encode(entry.getValue(), encode))
                            .append("&");//请求的参数之间使用&分割。
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }
            buffer.deleteCharAt(buffer.length() - 1);
            System.out.println(buffer.toString());
            try {
                HttpURLConnection urlConnection = (HttpURLConnection) url
                        .openConnection();
                urlConnection.setConnectTimeout(3000);
                //设置允许输入输出
                urlConnection.setDoInput(true);
                urlConnection.setDoOutput(true);
                byte[] mydata = buffer.toString().getBytes();
                //设置请求报文头,设定请求数据类型
                urlConnection.setRequestProperty("Content-Type",
                        "application/x-www-form-urlencoded");
                //设置请求数据长度
                urlConnection.setRequestProperty("Content-Length",
                        String.valueOf(mydata.length));
                //设置POST方式请求数据
                urlConnection.setRequestMethod("POST");
                OutputStream outputStream = urlConnection.getOutputStream();
                outputStream.write(mydata);
                int responseCode = urlConnection.getResponseCode();
                if (responseCode == 200) {
                    return changeInputStream(urlConnection.getInputStream(),
                            encode);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "";
    }

    /**
     * 把服务端返回的输入流转换成字符串格式
     * @param inputStream 服务器返回的输入流
     * @param encode 编码格式
     * @return 解析后的字符串
     */
    private static String changeInputStream(InputStream inputStream,
            String encode) { 
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] data = new byte[1024];
        int len = 0;
        String result="";
        if (inputStream != null) {
            try {
                while ((len = inputStream.read(data)) != -1) {
                    outputStream.write(data,0,len);                    
                }
                result=new String(outputStream.toByteArray(),encode);

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        //通过Map设置请求字符串。
        Map<String, String> params = new HashMap<String, String>();
        params.put("username", "admin");
        params.put("password", "123");        
        String result=sendPostMessage(params, "utf-8");
        System.out.println(result);
    }

}
原文地址:https://www.cnblogs.com/double1/p/5837926.html