码迷,mamicode.com
首页 > 移动开发 > 详细

Android之HttpClient的使用方法

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

标签:android

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的使用方法

标签:android

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!