android客户端+java服务器端
服务器端代码
@WebServlet("/PrintServlet")
public class PrintServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
String age = new String(request.getParameter("age").getBytes("ISO-8859-1"),"UTF-8");
System.out.println("姓名:"+name+"\n年龄:"+age);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
System.out.println("姓名:"+request.getParameter("name")+"\n年龄:"+request.getParameter("age"));
}
}
Android权限:
<uses-permission android:name="android.permission.INTERNET"/>
private OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
try{
if (getbutton == v) {
/*
* 因为是GET请求,所以需要将请求参数添加到URL后,并且还需要进行URL编码
* URL = http://192.168.0.103:8080/Server/PrintServlet?name=%E6%88%91&age=20
* 此处需要进行URL编码因为浏览器提交时自动进行URL编码
* */
StringBuilder buf = new StringBuilder("http://192.168.0.103:8080/Server/PrintServlet");
buf.append("?");
buf.append("name="+URLEncoder.encode(name.getText().toString(),"UTF-8")+"&");
buf.append("age="+URLEncoder.encode(age.getText().toString(),"UTF-8"));
URL url = new URL(buf.toString());
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
if(conn.getResponseCode()==200){
Toast.makeText(MainActivity.this, "GET提交成功", Toast.LENGTH_SHORT).show();
}
else Toast.makeText(MainActivity.this, "GET提交失败", Toast.LENGTH_SHORT).show();
}
if (postbutton == v) {
/*
* 如果是POST请求,则请求参数放在请求体中,
* name=%E6%88%91&age=12
*
* */
StringBuilder buf = new StringBuilder();
buf.append("name="+URLEncoder.encode(name.getText().toString(),"UTF-8")+"&");
buf.append("age="+URLEncoder.encode(age.getText().toString(),"UTF-8"));
byte[]data = buf.toString().getBytes("UTF-8");
URL url = new URL("http://192.168.0.103:8080/Server/PrintServlet");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true); //如果要输出,则必须加上此句
OutputStream out = conn.getOutputStream();
out.write(data);
if(conn.getResponseCode()==200){
Toast.makeText(MainActivity.this, "GET提交成功", Toast.LENGTH_SHORT).show();
}
else Toast.makeText(MainActivity.this, "GET提交失败", Toast.LENGTH_SHORT).show();
}
}
catch(Exception e){
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
name = (EditText) this.findViewById(R.id.name);
age = (EditText) this.findViewById(R.id.age);
getbutton = (Button) this.findViewById(R.id.getbutton);
postbutton = (Button) this.findViewById(R.id.postbutton);
getbutton.setOnClickListener(listener);
postbutton.setOnClickListener(listener);
}
}
三种提交方式的区别:
GET提交数据:
public void loginByGet(View view) {
String uname = et_main_uname.getText().toString();
String upass = et_main_upass.getText().toString();
String path = getString(R.string.server_name) + "login.xhtml";
//可变数组
new MyTask().execute(uname, upass, path, "GET");
}
POST提交:
public void loginByPost(View view) {
String uname = et_main_uname.getText().toString();
String upass = et_main_upass.getText().toString();
String path = getString(R.string.server_name) + "login.xhtml";
//可变数组
new MyTask().execute(uname, upass, path, "POST");
}
AsyncHttpClient属于android的开源框架:使用前导入相对于的包
public void loginByAsyncHttpClient(View view) {
String uname = et_main_uname.getText().toString();
String upass = et_main_upass.getText().toString();
String path = getString(R.string.server_name) + "login.xhtml";
AsyncHttpClient asyncHttpClient=new AsyncHttpClient();
RequestParams requestParams=new RequestParams();
requestParams.put("uname",uname);
requestParams.put("upass",upass);
asyncHttpClient.post(path,requestParams,new TextHttpResponseHandler(){
@Override
public void onSuccess(int statusCode, Header[] headers, String responseBody) {
super.onSuccess(statusCode, headers, responseBody);
Toast.makeText(MainActivity.this, responseBody, Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(int statusCode, Header[] headers, String responseBody, Throwable error) {
super.onFailure(statusCode, headers, responseBody, error);
}
});
}