使用HttpClient提交数据(get)
public void click1(View view) throws Exception{
final String username = et_text1.getText().toString().trim();
final String password = et_text2.getText().toString().trim();
if (TextUtils.isEmpty(username)&&TextUtils.isEmpty(password)) {
Toast.makeText(this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
return;
}
new Thread(new Runnable() {
@Override
public void run() {
try {
String path = "http://192.168.17.12:8080/http/loginServlet";
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(path+"?username="+URLEncoder.encode(username)+"&password="+password);
HttpResponse response = client.execute(request);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (200==statusCode) {
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
String data = StreamUtils.stream2string(inputStream);
handler.obtainMessage(RESULT_OK, data).sendToTarget();
}else {
handler.obtainMessage(RESULT_CANCELED, "handler发送信息失败!").sendToTarget();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
使用HttpClient提交数据(post)
public void click2(View view){
final String username = et_text1.getText().toString().trim();
final String password = et_text2.getText().toString().trim();
if (TextUtils.isEmpty(username)&&TextUtils.isEmpty(password)) {
Toast.makeText(this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
return;
}
new Thread(new Runnable() {
@Override
public void run() {
try {
String path = "http://192.168.17.12:8080/http/loginServlet";
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(path);
List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
parameters.add(new BasicNameValuePair("username", username));
parameters.add(new BasicNameValuePair("password", password));
HttpEntity entity = new UrlEncodedFormEntity(parameters );
request.setEntity(entity );
HttpResponse response = client.execute(request);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (200==statusCode) {
HttpEntity entity2 = response.getEntity();
InputStream content = entity2.getContent();
final String data = StreamUtils.stream2string(content);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,data+"提交成功", Toast.LENGTH_SHORT).show();
}
});
}else {
HttpEntity entity2 = response.getEntity();
InputStream content = entity2.getContent();
final String data = StreamUtils.stream2string(content);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,data+"提交失败!!!", Toast.LENGTH_SHORT).show();
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
public class StreamUtils {
public static String stream2string(InputStream is) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len=-1;
byte[] buffer = new byte[1024];
while((len=is.read(buffer))!=-1){
bos.write(buffer, 0, len);
}
is.close();
bos.close();
return bos.toString();
}
}
使用HttpURLConnection提交数据(get)
//get提交
public void click1(View view) throws Exception{
final String username = et_text1.getText().toString().trim()
final String password = et_text2.getText().toString().trim()
if (TextUtils.isEmpty(username)&&TextUtils.isEmpty(password)) {
Toast.makeText(this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show()
return
}
new Thread(new Runnable() {
@Override
public void run() {
try {
//http://localhost:8080/http/loginServlet?username=123&password=123
String path = "http://192.168.37.2:8080/http/loginServlet?username="+URLEncoder.encode(username)+"&password="+password
URL url = new URL(path)
HttpURLConnection conn = (HttpURLConnection) url.openConnection()
conn.setRequestMethod("GET")
conn.setReadTimeout(TIME_OUT)
conn.setConnectTimeout(TIME_OUT2)
conn.connect()
int responseCode = conn.getResponseCode()
if (200==responseCode) {
InputStream is = conn.getInputStream()
String data = StreamUtils.stream2string(is)
handler.obtainMessage(RESULT_OK, data).sendToTarget()
}else {
handler.obtainMessage(RESULT_CANCELED, "handler发送信息失败!").sendToTarget()
}
} catch (Exception e) {
e.printStackTrace()
}
}
}).start()
}
使用HttpURLConnection提交数据(post)
//post提交
public void click2(View view){
final String username = et_text1.getText().toString().trim()
final String password = et_text2.getText().toString().trim()
if (TextUtils.isEmpty(username)&&TextUtils.isEmpty(password)) {
Toast.makeText(this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show()
return
}
new Thread(new Runnable() {
@Override
public void run() {
try {
String path = "http://192.168.37.2:8080/http/loginServlet"
URL url = new URL(path)
HttpURLConnection conn = (HttpURLConnection) url.openConnection()
conn.setRequestMethod("POST")
conn.setDoOutput(true)
conn.setReadTimeout(TIME_OUT)
conn.setConnectTimeout(TIME_OUT2)
conn.connect()
//打开一个流给服务器提交数据
OutputStream os = conn.getOutputStream()
String param = "username="+URLEncoder.encode(username)+"&password="+password
os.write(param.getBytes())
int responseCode = conn.getResponseCode()
if (200==responseCode) {
InputStream is = conn.getInputStream()
String data = StreamUtils.stream2string(is)
handler.obtainMessage(RESULT_OK, data).sendToTarget()
}else {
handler.obtainMessage(RESULT_CANCELED, "handler发送信息失败!").sendToTarget()
}
} catch (Exception e) {
e.printStackTrace()
}
}
}).start()
}
HttpClient与HttpURLConnection区别
1、HttpClient是apache的开源实现,而HttpUrlConnection是安卓标准实现,安卓SDK虽然集成了HttpClient,但官方支持的却是HttpUrlConnection;
2、HttpUrlConnection直接支持GZIP压缩;HttpClient也支持,但要自己写代码处理;
3、HttpUrlConnection直接在系统层面做了缓存策略处理,加快重复请求的速度。