首页 > 移动平台 > 详细

Android之HttpClient的使用方法

时间:2015-08-18 22:54:22      阅读:287      评论:0      收藏:0      [点我收藏+]

HttpClient的使用方法

实例解析:HttpClient的get,post的数据传输

package com.example.httpclient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLEncoder;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
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.AsyncTask;
import android.os.Bundle;



public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		AsyncTask.execute(new Runnable() {

			@Override
			public void run() {
				try {
					callByGet();
				} catch (Exception e) {
				}
			}
		});
	}
	public  void callByGet() throws Exception {
		HttpClient client = new DefaultHttpClient();
		HttpGet get= new HttpGet(
				"http://192.168.48.176:8081/myweb/CheckGet?name=aaa&pwd="+URLEncoder.encode("你好", "utf-8"));
		//重新编码,解决中文乱码问题
		HttpResponse response = client.execute(get);//向服务器发送请求,获取响应数据

		HttpEntity entity = response.getEntity();//把相应数据包装成HttpEntity对象,方便打印

		System.out.println(EntityUtils.toString(entity));

		//获得内容转换成页面存储起来:
		BufferedReader reader = new BufferedReader(
				new InputStreamReader(entity.getContent()));



	}
	public void  callByPost(){

	}
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

Android之HttpClient的使用方法

原文:http://blog.csdn.net/wei_chong_chong/article/details/47759875

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