首页 > 移动平台 > 详细

Android使用Apache HttpClient发送GET、POST请求

时间:2014-03-25 11:51:42      阅读:490      评论:0      收藏:0      [点我收藏+]
简单的网页下载,HttpURLConnection可以完成,但是涉及到用户登录等权限相关问题,就需要涉及Session、Cookies。,就很难使用HttpURLConnection来处理了。Apache开源组织提供了一个HttpClient项目可以处理这些问题。HttpClient关注于如何发送请求、接受请求,以及管理HTTP链接。
使用HttpClient对象来发送请求、接受响应步骤:


创建HttpClient对象
如果要发送GET请求,创建HttpGet对象;如果是POST请求,则创建HttpPost对象。
如果需要添加参数,对于HttpGet直接在构造URL的时候填入参数。对于POST请求,使用setEntity(HttpEntity entity)方法来设置
调用HttpClient对象的execute(HttpUriRequest request)发送请求,此方法返回一个HttpResponse
调用HttpResponse的getALLHeaders()、getHeaders(String name)等方法可获取服务器响应头;调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器响应内容。
注意:


不少地方说可以使用HttpGet和HttpPost共同的setParams(HttpParams params)方法添加请求参数,但是我没有设置成功,网上搜索发现好多人也没成功。Even Apache’s official example uses URIBuilder’s setParameter method to build the params out in the URI,所以没有使用这种方法.


GET请求Demo:

public class MainActivity extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView textViewShow = (TextView) findViewById(R.id.showText);
        //直接在URL后添加请求参数
        String url = "http://192.168.1.103/index.php?get1=hello&get2=bay";
        try {
            // 创建DefaultHttpClient对象
            HttpClient httpclient = new DefaultHttpClient();
            // 创建一个HttpGet对象
            HttpGet get = new HttpGet(url);
            // 获取HttpResponse对象
            HttpResponse response = httpclient.execute(get);
            //判断是否链接成功
            if (response.getStatusLine().getStatusCode() == 200) {
                //实体转换为字符串
                String content = EntityUtils.toString(response.getEntity(),"utf-8");
                textViewShow.setText(content);
            }else{
                textViewShow.setText("网络错误");
            }
 
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}
POST请求Demo:

public class MainActivity extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView textViewShow = (TextView) findViewById(R.id.showText);
 
        String url = "http://192.168.1.103/index.php";
        HttpClient httpClient = new DefaultHttpClient();
        try {
        HttpPost post = new HttpPost(url);
        List params = new ArrayList();
        params.add(new BasicNameValuePair("get1", "hello"));
        params.add(new BasicNameValuePair("get2", "usrl"));
 
            post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
            HttpResponse response = httpClient.execute(post);
            if(response.getStatusLine().getStatusCode() ==200){
                String content = EntityUtils.toString(response.getEntity(),"utf-8");
                textViewShow.setText(content);
 
            }else{
                textViewShow.setText("网络问题");
            }
 
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            textViewShow.setText("UnsupportedEncodingException");
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            textViewShow.setText("ClientProtocolException");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            textViewShow.setText("IOException");
        }
 
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
 
}



Android使用Apache HttpClient发送GET、POST请求,布布扣,bubuko.com

Android使用Apache HttpClient发送GET、POST请求

原文:http://blog.csdn.net/chenyiming_1990/article/details/21956489

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!