HTTPClient

<span style="font-size:18px;">package com.example.httpclient;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

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

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	EditText username;
	EditText password;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		username = (EditText) findViewById(R.id.et_username);
		password = (EditText) findViewById(R.id.et_password);
		
	}
	
	
	public void getSubmit(View view)
	{
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				//1.定义一个客户端 你可以理解为是一个浏览器
				HttpClient client = new DefaultHttpClient();
				
				//2. 定义一个get请求,并封装好他的参数
				String data = "username=" + username.getText().toString() + "&password=" + password.getText().toString();
				HttpGet get = new HttpGet("http://10.0.2.2:8080/TestHttp/HttpServlet?" + data);
				
				//3. 用定义好的浏览器来执行一个地址
				try {
					HttpResponse response = client.execute(get);
					//4. 获取状态码,看返回回来的是否是200
					final int statuscode = response.getStatusLine().getStatusCode();
					if(200 == statuscode)
					{
						InputStream in = response.getEntity().getContent();
						
						//处理从服务器端返回回来的数据
						final String content = getStringFromStream(in);
						
						runOnUiThread(new Runnable() {
							
							@Override
							public void run() {
								Toast.makeText(MainActivity.this, "状态码为:" + statuscode +" 内容为:" + content, 0).show();
							}
						});
					}
					
				} catch (ClientProtocolException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}finally
				{
					//5. 用完以后关闭浏览器
					if(null != client)
					{
						client.getConnectionManager().shutdown();
					}
				}
			}
		}).start();
		
	}
	
	public void postSubmit(View view)
	{
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				//1.顶一个一个浏览器对象
				HttpClient client = new DefaultHttpClient();
				//2. 定义一个post请求的对象
				HttpPost post = new HttpPost("http://10.0.2.2:8080/TestHttp/HttpServlet");
				
				List<NameValuePair> parameters = new ArrayList<NameValuePair>();
				
				NameValuePair pair_name = new BasicNameValuePair("username", username.getText().toString());
				NameValuePair pair_pwd = new BasicNameValuePair("password", password.getText().toString());
				
				parameters.add(pair_name);
				parameters.add(pair_pwd);
				
				//3.执行一个请求
				try {
					UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters, "utf-8");
					post.setEntity(entity);
					
					HttpResponse response = client.execute(post);
					
					final int statuscode = response.getStatusLine().getStatusCode();
					if(200 == statuscode)
					{
						InputStream in = response.getEntity().getContent();
						
						//处理从服务器端返回回来的数据
						final String content = getStringFromStream(in);
						
						runOnUiThread(new Runnable() {
							
							@Override
							public void run() {
								Toast.makeText(MainActivity.this, "状态码为:" + statuscode +" 内容为:" + content, 0).show();
							}
						});
					}
					
					
				} catch (ClientProtocolException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
			}
		}).start();
	}
	
	public String getStringFromStream(InputStream in) throws IOException
	{
		byte[] buffer = new byte[1024];
		ByteArrayOutputStream bytearray = new ByteArrayOutputStream();
		int len = 0;
		while((len = in.read(buffer, 0, 1024)) != -1)
		{
			bytearray.write(buffer);
		}
		
		String content = bytearray.toString();
		bytearray.close();
		
		return content;
	}
}
</span>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值