package com.example.httpclient; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.EditText; import android.widget.Toast; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.StatusLine; 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 java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; public class MyActivity extends Activity { private EditText et1; private EditText et2; private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ case 1: String result = (String) msg.obj; Toast.makeText(MyActivity.this,result,Toast.LENGTH_SHORT).show(); break; case 2: String o = (String) msg.obj; Toast.makeText(MyActivity.this,o,Toast.LENGTH_SHORT).show(); break; } } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); et1 = (EditText) findViewById(R.id.et1); et2 = (EditText) findViewById(R.id.et2); findViewById(R.id.get).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Thread thread = new Thread(){ @Override public void run() { String username = et1.getText().toString(); String password = et2.getText().toString(); String path = "http://192.168.21.1:8080/ok/servlet/Login?username="+username+"&password="+password; //创建HttpClient对象 HttpClient httpClient = new DefaultHttpClient(); //创建get方法 HttpGet httpGet = new HttpGet(path); try { //使用客户端对象用get方式提交到服务器 HttpResponse httpResponse = httpClient.execute(httpGet); //得到状态行,这里有响应码等信息 StatusLine statusLine = httpResponse.getStatusLine(); //得到响应码 if(statusLine.getStatusCode()==200){ //得到响应的实体,这里有输入输出流信息 HttpEntity httpEntity = httpResponse.getEntity(); InputStream inputStream = httpEntity.getContent(); String data = getDataFromIn(inputStream); Message msg = handler.obtainMessage(); msg.what = 1; msg.obj = data; handler.sendMessage(msg); } } catch (Exception e) { e.printStackTrace(); } } }; thread.start(); } }); findViewById(R.id.post).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Thread thread = new Thread(){ @Override public void run() { String username = et1.getText().toString(); String password = et2.getText().toString(); String path = "http://192.168.21.1:8080/ok/servlet/Login"; //创建客户端对象 HttpClient httpClient = new DefaultHttpClient(); //创建post方法 HttpPost httpPost = new HttpPost(path); //声明并实例化list存放basicNameValuePair List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>(); //把要提交的值放到namevaluepair中 BasicNameValuePair basicNameValuePair = new BasicNameValuePair("username",username); BasicNameValuePair basicNameValuePair2 = new BasicNameValuePair("password",password); list.add(basicNameValuePair); list.add(basicNameValuePair2); try { //表单提交的实体这个方法需要两个参数一个是list<namevaluepair> 还有一个编码集 UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list,"utf-8"); //设置实体 httpPost.setEntity(urlEncodedFormEntity); //客户端对象使用post方式提交 HttpResponse httpResponse = httpClient.execute(httpPost); StatusLine statusLine = httpResponse.getStatusLine(); if(statusLine.getStatusCode()==200){ HttpEntity httpEntity = httpResponse.getEntity(); InputStream in = httpEntity.getContent(); String ok = getDataFromIn(in); Message msg = handler.obtainMessage(); msg.what = 2; msg.obj =ok; handler.sendMessage(msg); } } catch (Exception e) { e.printStackTrace(); } } }; thread.start(); } }); } private static String getDataFromIn(InputStream in){ ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] b = new byte[1024]; int len = 0; try { while ((len = in.read(b))!=-1){ out.write(b,0,len); } } catch (IOException e) { e.printStackTrace(); } return new String(out.toByteArray()); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:id="@+id/et1" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入用户名"/> <EditText android:id="@+id/et2" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码"/> <Button android:id="@+id/get" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="get"/> <Button android:id="@+id/post" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="post" /> </LinearLayout>
时间: 2024-10-10 06:07:24