朋友让写一个关于android与server端servlet交互的demo,比项目中的例子要简单很多。入门的朋友可以参考下。核心代码:01/**02 *03 * 项目名称 JSONDemo04 * 包 名 servlet05 * 文 件 名 CustomerServlet.java06 * 开 发 人 Administrator07 * 描述信息 客户端验证用户…
朋友让写一个关于android与server端servlet交互的demo,比项目中的例子要简单很多。
入门的朋友可以参考下。
核心代码:
05 |
* 文 件 名 CustomerServlet.java |
07 |
* 描述信息 客户端验证用户登陆Servlet |
08 |
* 发布日期 2012-4-6下午03:28:47 |
14 |
public class CustomerServlet extends HttpServlet { |
16 |
private static final long serialVersionUID = 314719472293387358L; |
19 |
protected void doPost(HttpServletRequest req, HttpServletResponse resp) |
20 |
throws ServletException, IOException { |
24 |
String username = req.getParameter("username"); |
25 |
String password = req.getParameter("password"); |
26 |
//调用UserDAO中isLogin方法判断数据中用户名密码是否正确 |
27 |
boolean flag=UserDAO.isLogin(username, password); |
29 |
DataOutputStream output=new DataOutputStream(resp.getOutputStream()); |
32 |
output.writeUTF("服务器端数据:"+LOGIN_FLAG); |
33 |
System.out.println(LOGIN_FLAG); |
39 |
System.out.println(LOGIN_FLAG); |
40 |
output.writeUTF("服务器端数据:"+LOGIN_FLAG); |
43 |
} catch (Exception e) { |
ClientDemo 核心代码:
001 |
public class MainActivity extends Activity { |
002 |
//private static final int REQUEST_CODE = 2; |
003 |
HttpPost httpRequest=new HttpPost(UriAPI.HTTPCustomer); |
009 |
ProgressDialog progressDialog; |
010 |
/** Called when the activity is first created. */ |
012 |
public void onCreate(Bundle savedInstanceState) { |
013 |
super.onCreate(savedInstanceState); |
014 |
setContentView(R.layout.main); |
016 |
btn_login=(Button) findViewById(R.id.btn_login); |
017 |
btn_cancle=(Button) findViewById(R.id.btn_cancle); |
018 |
et_name=(EditText) findViewById(R.id.et_name); |
019 |
et_pwd = (EditText) findViewById(R.id.et_pwd); |
020 |
show_login=(TextView) findViewById(R.id.show_login); |
022 |
progressDialog = new ProgressDialog(this); |
023 |
btn_login.setOnClickListener(new OnClickListener() { |
025 |
@SuppressWarnings("unchecked") |
027 |
public void onClick(View v) { |
028 |
//通过AsyncTask类提交数据 异步显示 |
029 |
new AT().execute(et_name.getText().toString(),et_pwd.getText().toString()); |
034 |
public class UriAPI { |
036 |
public static final String HTTPCustomer ="http://10.0.1.9:8026/JSONDemo/servlet/CustomerServlet"; |
038 |
@SuppressWarnings("rawtypes") |
039 |
class AT extends AsyncTask{ |
043 |
protected void onPreExecute() { |
045 |
progressDialog.show(); |
049 |
protected Object doInBackground(Object... params_obj) { |
050 |
CharSequence username=""; |
051 |
CharSequence password=""; |
053 |
username=et_name.getText(); |
055 |
password =et_pwd.getText(); |
056 |
if(!username.equals("")&&!password.equals("")){ |
058 |
HttpPost httpRequest = new HttpPost(UriAPI.HTTPCustomer); |
060 |
List<NameValuePair> params=new ArrayList<NameValuePair>(); |
061 |
params.add(new BasicNameValuePair("username", username.toString())); |
062 |
params.add(new BasicNameValuePair("password", password.toString())); |
063 |
//params.add(new BasicNameValuePair("flag","0")); |
066 |
httpRequest.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8)); |
067 |
HttpResponse httpResponse=new DefaultHttpClient().execute(httpRequest); |
069 |
if (httpResponse.getStatusLine().getStatusCode()==200) { |
071 |
byte[] data =new byte[2048]; |
073 |
data =EntityUtils. toByteArray((HttpEntity)httpResponse.getEntity()); |
075 |
ByteArrayInputStream bais = new ByteArrayInputStream(data); |
077 |
DataInputStream dis = new DataInputStream(bais); |
078 |
//将字节数组中的数据还原成原来的各种数据类型,代码如下: |
079 |
result=new String(dis.readUTF()); |
080 |
Log.i("服务器返回信息:", result); |
082 |
} catch(ClientProtocolException e){ |
084 |
}catch(UnsupportedEncodingException e){ |
086 |
} catch (IOException e) { |
095 |
protected void onPostExecute(Object result) { |
098 |
show_login.setText(result.toString()); |
100 |
progressDialog.cancel(); |
界面截图
1.未登录

2.登陆中

3.登陆成功

4.登陆失败
