android HttpClient连接Web端

MainActivity.java
package com.httpclient.test_01;

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

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
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.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;


import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

	private Button button;
	private MyHandler handler;

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

		button = (Button) findViewById(R.id.button1);
		button.setOnClickListener(this);
		
		handler=new MyHandler();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v.getId() == R.id.button1) {
			MyThread thread=new MyThread();
			thread.start();
		}
	}
	class MyHandler extends Handler{
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
		}
	}

	class MyThread extends Thread {
		@Override
		public void run() {
			try {
				String url = "http://192.168.1.101:8080/com.my_store/loginServlet";
				HttpClient client = new DefaultHttpClient();

				HttpPost request = new HttpPost(url);
				List<NameValuePair> list = new ArrayList<NameValuePair>();
				list.add(new BasicNameValuePair("name", "jack"));
				list.add(new BasicNameValuePair("password", "111"));
				list.add(new BasicNameValuePair("android", "android"));
				request.setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8));

				HttpResponse response = client.execute(request);
				if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
					String islogin=EntityUtils.toString(response.getEntity());
					if("success".equals(islogin))
					handler.sendMessage(new Message());
					System.out.println(islogin);
				}

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}
}

activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="136dp"
        android:text="httpclient登录" />

</RelativeLayout>


manifest.xml
    <uses-permission android:name="android.permission.INTERNET"/>

你可能感兴趣的:(httpclient)