当前位置:操作系统 > 安卓/Android >>

Android入门:用HttpClient模拟HTTP的GET和POST请求


一、HttpClient介绍

HttpClient是用来模拟HTTP请求的,其实实质就是把HTTP请求模拟后发给Web服务器;

Android已经集成了HttpClient,因此可以直接使用;

注:此处HttpClient代码不只可以适用于Android,也可适用于一般的Java程序;

HTTP GET核心代码:

(1)DefaultHttpClient client = new DefaultHttpClient();
(2)HttpGet get = new HttpGet(String url);//此处的URL为http://..../path?arg1=value&....argn=value
(3)HttpResponse response = client.execute(get); //模拟请求
(4)int code = response.getStatusLine().getStatusCode();//返回响应码
(5)InputStream in = response.getEntity().getContent();//服务器返回的数据


HTTP POST核心代码:

(1)DefaultHttpClient client = new DefaultHttpClient();
(2)BasicNameValuePair pair = new BasicNameValuePair(String name,String value);//创建一个请求头的字段,比如content-type,text/plain
(3)UrlEncodedFormEntity entity = new UrlEncodedFormEntity(List<NameValuePair> list,String encoding);//对自定义请求头进行URL编码
(4)HttpPost post = new HttpPost(String url);//此处的URL为http://..../path
(5)post.setEntity(entity);
(6)HttpResponse response = client.execute(post);
(7)int code = response.getStatusLine().getStatusCode();
(8)InputStream in = response.getEntity().getContent();//服务器返回的数据

二、服务器端代码

服务器端代码和通过URLConnection发出请求的代码不变:

[java] 
package org.xiazdong.servlet; 
 
import java.io.IOException; 
import java.io.OutputStream; 
 
import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
@WebServlet("/Servlet1") 
public class Servlet1 extends HttpServlet { 
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
        String nameParameter = request.getParameter("name"); 
        String ageParameter = request.getParameter("age"); 
        String name = new String(nameParameter.getBytes("ISO-8859-1"),"UTF-8"); 
        String age = new String(ageParameter.getBytes("ISO-8859-1"),"UTF-8"); 
        System.out.println("GET"); 
        System.out.println("name="+name); 
        System.out.println("age="+age); 
        response.setCharacterEncoding("UTF-8"); 
        OutputStream out = response.getOutputStream();//返回数据 
        out.write("GET请求成功!".getBytes("UTF-8")); 
        out.close(); 
    } 
 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
        request.setCharacterEncoding("UTF-8"); 
        String name = request.getParameter("name"); 
        String age  = request.getParameter("age"); 
        System.out.println("POST"); 
        System.out.println("name="+name); 
        System.out.println("age="+age); 
        response.setCharacterEncoding("UTF-8"); 
        OutputStream out = response.getOutputStream(); 
        out.write("POST请求成功!".getBytes("UTF-8")); 
        out.close(); 
         
    } 
 

 

三、Android客户端代码

效果如下:

 


在AndroidManifest.xml加入:

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


MainActivity.java

[java] 
package org.xiazdong.network.httpclient; 
 
import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.URLEncoder; 
import java.util.ArrayList; 
import java.util.List; 
 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
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.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 
 
public class MainActivity extends Activity { 
    private EditText name, age; 
    private Button getbutton, postbutton; 
    private OnClickListener listener = new OnClickListener() { 
        @Override 
        public void onClick(View v) { 
            try{ 
                if(postbutton==v){ 
                    /*
                     * NameValuePair代表一个HEADER,List<NameValuePair>存储全部的头字段
                     * UrlEncodedFormEntity类似于URLEncoder语句进行URL编码
                     * HttpPost类似于HTTP的POST请求
                     * client.execute()类似于发出请求,并返回Response
                     */ 
                    DefaultHttpClient client = new DefaultHttpClient(); 
                 &

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,