package com.example.android_login;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
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 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;
public class MainActivity extends Activity {
protected static final int SUCCESS = 1;
private EditText et_username;
private EditText et_password;
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case SUCCESS:
String content = (String) msg.obj;
Toast.makeText(MainActivity.this, content, 0).show();
break;
default:
break;
}
};
};
// http://192.168.137.218:8080/LoginTest/servlet/LoginServlet?username=zhangsan&password=123
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_username = (EditText) findViewById(R.id.et_username);
et_password = (EditText) findViewById(R.id.et_password);
}
/**
* get提交
*
* @param v
*/
public void getLogin(View v) {
try {
String password = et_password.getText().toString().trim();
String username = et_username.getText().toString().trim();
String path = "http://192.168.137.218:8080/LoginTest/servlet/LoginServlet?username="
+ URLEncoder.encode(username, "utf-8")
+ "&password="
+ URLEncoder.encode(password, "utf-8");
// get请求
getLogin(path);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
private void getLogin(final String path) {
new Thread() {
public void run() {
try {
// 定义一个Http客户端对象
HttpClient httpClient = new DefaultHttpClient();
// 定义一个get请求的对象
HttpGet httpGet = new HttpGet(path);
// 执行get请求,获取响应
HttpResponse httpResponse = httpClient.execute(httpGet);
// 先获取状态行,再获取响应码
int statusCode = httpResponse.getStatusLine()
.getStatusCode();
if (statusCode == 200) {
// 请求成功
// 获取实体对象
HttpEntity entity = httpResponse.getEntity();
// 获取实体内容流
InputStream inputStream = entity.getContent();
String str = steamToStr(inputStream);
Message obtain = Message.obtain();
obtain.obj = str;
obtain.what = SUCCESS;
handler.sendMessage(obtain);
}
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
}
public void postLogin(View v) {
String password = et_password.getText().toString().trim();
String username = et_username.getText().toString().trim();
String path = "http://192.168.137.218:8080/LoginTest/servlet/LoginServlet";
postLogin(path, username, password);
}
/**
* post提交
*
* @param path
* @param username
* @param password
*/
private void postLogin(final String path, final String username,
final String password) {
new Thread() {
public void run() {
try {
// 创建一个客户端对象
HttpClient httpClient = new DefaultHttpClient();
// 创建post请求对象
HttpPost httpPost = new HttpPost(path);
// 创建一个参数的集合
List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
// 添加参数 键值对方式
parameters
.add(new BasicNameValuePair("username", username));
parameters
.add(new BasicNameValuePair("password", password));
// 实例化实体对象
HttpEntity httpEntity = new UrlEncodedFormEntity(parameters);
// 设置实体内容
httpPost.setEntity(httpEntity);
// 执行post请求 获取响应
HttpResponse httpResponse = httpClient.execute(httpPost);
// 获取状态行对象
StatusLine statusLine = httpResponse.getStatusLine();
// 获取状态码
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
// 获取实体对象
HttpEntity entity = httpResponse.getEntity();
// 获取实体内容
InputStream inputStream = entity.getContent();
//转成字符串
String str = steamToStr(inputStream);
//发送给主线程
Message obtain = Message.obtain();
obtain.obj = str;
obtain.what = SUCCESS;
handler.sendMessage(obtain);
}
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
}
/**
* 将输入流转成字符串
*
* @param inputStream
* @return
* @throws IOException
*/
private String steamToStr(InputStream inputStream) throws IOException {
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
arrayOutputStream.write(buffer, 0, len);
}
String content = arrayOutputStream.toString();
return content;
};
}
HttpClient网络请求
最新推荐文章于 2024-08-06 17:13:24 发布