三种方式提交数据至服务器验证:
(登录案列)
布局文件
服务器端 (利用Tomcat发布)
package com.sun.servlet;
import java.io.IOException;
import java.io.Writer;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.biz.Dtb_login_biz;
import com.sun.biz.Dtb_login_bizImp;
import com.sun.biz.Dtb_users_biz;
import com.sun.biz.Dtb_users_bizImp;
import com.sun.entity.Dtb_login;
import com.sun.entity.Dtb_users;
public class LoginServlet extends HttpServlet{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String aname=req.getParameter("aname");
String apass=req.getParameter("apass");
Dtb_login dtb_login=new Dtb_login(aname, apass);
String result=null;
Dtb_login_biz login_biz=new Dtb_login_bizImp();
Writer out =resp.getWriter();
if(login_biz.Login(dtb_login)){ //判断后返回你想要返回的数据
result="true";
}else{
result="falseOne";
}
out.write(result);
out.flush();
}
}
注意:如果是get提交的话,只需要将doPost 改成doGet 即可。
一、原生态get提交(不提倡:get方式对数据及的保护性不够高)
package com.example.android32_submit;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import org.apache.http.Header;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private EditText et_aname;
private EditText et_apass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取控件
et_aname = (EditText) findViewById(R.id.et_aname);
et_apass = (EditText) findViewById(R.id.et_apass);
}
//get方式提交数据去服务器验证
public void getSubmit(View view){
//获取输入框的用户名和密码
String aname=et_aname.getText().toString();
String apass=et_apass.getText().toString();
//服务器验证路径
String path="http://192.168.43.210:8080/T2/alogin.do";
new MyGetTask().execute(aname,apass,path);
}
class MyGetTask extends AsyncTask{
@Override
protected Object doInBackground(Object[] objects) {
//获取传过来的参数
String aname=objects[0].toString(); //用户名
String apass=objects[1].toString(); //密码
String path=objects[2].toString(); //请求路径
try {
URL url=new URL(path);
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
//设置请求方式
conn.setRequestMethod("GET");
//设置响应时间
conn.setConnectTimeout(5000);
if(conn.getResponseCode()==200){//判断结果码
InputStream is=conn.getInputStream(); //打开输入流
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String s=br.readLine();
return s;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
String s= (String) o;
Toast.makeText(MainActivity.this, ""+s, Toast.LENGTH_SHORT).show();
}
}
二、原生态的post提交
//post方式提交数据去服务器验证
public void postSubmit(View view){
//获取输入框的用户名和密码
String aname=et_aname.getText().toString();
String apass=et_apass.getText().toString();
//服务器验证路径
String path="http://192.168.43.210:8080/T2/alogin.do";
new MyPostTask().execute(aname,apass,path);
}
class MyPostTask extends AsyncTask{
@Override
protected Object doInBackground(Object[] objects) {
//获取传过来的参数
String aname=objects[0].toString(); //用户名
String apass=objects[1].toString(); //密码
String path=objects[2].toString(); //请求路径
try {
URL url=new URL(path);
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
//设置请求方式
conn.setRequestMethod("POST");
//设置响应时间
conn.setConnectTimeout(5000);
String str="aname="+aname+"&apass="+apass;
//添加请求头
conn.setRequestProperty("Content-Length",str.length()+"");
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
//设置允许对外输出数据
conn.setDoOutput(true);
OutputStream os=conn.getOutputStream();
os.write(str.getBytes());
if(conn.getResponseCode()==200){//判断结果码
InputStream is=conn.getInputStream(); //打开输入流
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String s=br.readLine();
return s;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
String s= (String) o;
Toast.makeText(MainActivity.this,s, Toast.LENGTH_SHORT).show();
}
}
三、第三方AsyncHttpClient 提交
下载地址:http://download.csdn.net/detail/a985548426/9913244
在Android端导入上面的架包,因为这个架包是依赖谷歌公式HttpClient的所以导入架包后还得在build.gradle中加入下图中画框的代码即可
//async第三方提交数据去服务器验证
public void asyncSubmit(View view){
//获取输入框的用户名和密码
String aname=et_aname.getText().toString();
String apass=et_apass.getText().toString();
//服务器验证路径
String path="http://192.168.43.210:8080/T2/alogin.do";
AsyncHttpClient ahc=new AsyncHttpClient();
RequestParams rp=new RequestParams();
rp.put("aname",aname);
rp.put("apass",apass);
ahc.post(this,path,rp,new TextHttpResponseHandler(){
@Override
public void onFailure(int statusCode, Header[] headers, String responseBody, Throwable error) {
super.onFailure(statusCode, headers, responseBody, error);
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseBody) {
super.onSuccess(statusCode, headers, responseBody);
Toast.makeText(MainActivity.this, responseBody, Toast.LENGTH_SHORT).show();
}
});
}
}