android httpclient get post 代码

package com.marschen.httpgetandpost;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

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

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {

	private EditText nameText;
	private EditText pwdText;
	private Button button;

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

		nameText = (EditText) findViewById(R.id.nameText);
		pwdText = (EditText) findViewById(R.id.pwdText);
		button = (Button) findViewById(R.id.submitButton);

		button.setOnClickListener(new ButtonListener());
	}

	class ButtonListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			String name = nameText.getText().toString();
			String pwd = pwdText.getText().toString();

			//使用GET方法向服务器发送请求
//			GetThread gt = new GetThread(name, pwd);
//			gt.start();
			
			//使用POST方法向服务器发送请求
			PostThread pt = new PostThread(name, pwd);
			pt.start();
		}
	}

	//该线程使用POST方法向服务器发送请求
	class PostThread extends Thread {

		String name;
		String pwd;

		public PostThread(String name, String pwd) {
			this.name = name;
			this.pwd = pwd;
		}

		@Override
		public void run() {
			HttpClient httpClient = new DefaultHttpClient();
			String url = "http://192.168.1.103:8080/s02e14.jsp";
			//生成使用POST方法的请求对象
			HttpPost httpPost = new HttpPost(url);
			//NameValuePair对象代表了一个需要发往服务器的键值对
			NameValuePair pair1 = new BasicNameValuePair("name", name);
			NameValuePair pair2 = new BasicNameValuePair("password", pwd);
			//将准备好的键值对对象放置在一个List当中
			ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();
			pairs.add(pair1);
			pairs.add(pair2);
			try {
				//创建代表请求体的对象
				HttpEntity requestEntity = new UrlEncodedFormEntity(pairs);
				//将请求体放置在请求对象当中
				httpPost.setEntity(requestEntity);
				//执行请求对象
				try {
					HttpResponse response = httpClient.execute(httpPost);
					if (response.getStatusLine().getStatusCode() == 200) {
						HttpEntity entity = response.getEntity();
						BufferedReader reader = new BufferedReader(
								new InputStreamReader(entity.getContent()));
						String result = reader.readLine();
						Log.d("HTTP", "POST:" + result);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	class GetThread extends Thread {

		String name;
		String pwd;

		public GetThread(String name, String pwd) {
			this.name = name;
			this.pwd = pwd;
		}

		@Override
		public void run() {
			HttpClient httpClient = new DefaultHttpClient();
			String url = "http://192.168.1.103:8080/s02e14.jsp?name=" + name+ "&password=" + pwd;
			HttpGet httpGet = new HttpGet(url);
			try {
				HttpResponse response = httpClient.execute(httpGet);
				if (response.getStatusLine().getStatusCode() == 200) {
					HttpEntity entity = response.getEntity();
					BufferedReader reader = new BufferedReader(
							new InputStreamReader(entity.getContent()));
					String result = reader.readLine();
					Log.d("HTTP", "GET:" + result);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值