android 以httpclient方式把数据提交到服务器
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
public class StreamTools {
//把一个inputStream 转换成一个String
public static String readStream(InputStream in) throws Exception{
//定义一个央存输出流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = -1;
byte[] butter = new byte[1024];
while((len = in.read(butter)) != -1){
baos.write(butter,0,len);
}
in.close();
String content = new String(baos.toByteArray());
return content;
}
}
package com.example.sdafds;
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.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText et_password;
private EditText et_username;
@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请求
public void click1(View v){
new Thread(){
public void run(){
try {
String name = et_username.getText().toString().trim();
String pwd = et_password.getText().toString().trim();
//2.1定义get方式提交的路经
String path = "http://192.168.11.73:8080/login/Login?LoginServlet?username="+URLEncoder.encode(name,"utf-8")+URLEncoder.encode(pwd,"utf-8")+"";
//3 获取httpclient实例
DefaultHttpClient client = new DefaultHttpClient();
//3.1准备get请求定义一个httpget实现
HttpGet get = new HttpGet(path);
//3.2 执行一个get请求
HttpResponse response = client.execute(get);
//4 获取服务器的返回状态码
int code = response.getStatusLine().getStatusCode();
if(code == 200){
//5获取服务器返回数据 以流的形式返回
InputStream inputStream = response.getEntity().getContent();
//6 把流转换成字符串
String content = StreamTools.readStream(inputStream);
showToast(content);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
//post 提交数据
public void click2(View v){
new Thread(){public void run(){
try {
//1 拿到用户名和用户密码
String name = et_username.getText().toString().trim();
String pwd = et_password.getText().toString().trim();
String path = "http://192.168.11.73:8080/login/LoginServlet";
//2 用 httpClient方式提交灵数据
DefaultHttpClient client = new DefaultHttpClient();
//3.1 准备post 请求
HttpPost post = new HttpPost(path);
//3.1.0 NameValuePair
List<NameValuePair> lists = new ArrayList<NameValuePair>();
//3.1.1准备NameValuePair实际上就是我们要提交的用户名和密码 key是服务器key:username
BasicNameValuePair nameValuePair = new BasicNameValuePair("username", name);
BasicNameValuePair pwdValuePair = new BasicNameValuePair("password", pwd);
//3.1.2 把nameValuePair 和 pwdValuePair 加入到集合中
lists.add(nameValuePair);
lists.add(pwdValuePair);
//3.1.3 准备entity
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(lists);
//3.2 准备post方式提交正文 以实体形式准备(建值对形式)
post.setEntity(entity);
//3.3 执行一个get请求
HttpResponse response = client.execute(post);
//4 获取服务器的返回状态码
int code = response.getStatusLine().getStatusCode();
if(code == 200){
//5获取服务器返回数据 以流的形式返回
InputStream inputStream = response.getEntity().getContent();
//6 把流转换成字符串
String content = StreamTools.readStream(inputStream);
showToast(content);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}.start();
}
//封装的提交更新UI函数(方法)
public void showToast(final String content){
runOnUiThread(new Runnable() {
@Override
public void run() {
// //该方法一定是执行主线程
Toast.makeText(getApplicationContext(), content, 1).show();
}
});
}
}
android 6.0 以后不能用httpclient方法要手动加载包