Android向服务器提交数据
来源:互联网 发布:阿里云os电视直播软件 编辑:程序博客网 时间:2024/05/07 16:28
首先我们先来 写服务器,这里就用登录来演示效果
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><body> <form action="loginResult.xhtml" method="post"> Uname:<input type="text" name="uname"/><br/> Upass:<input type="text" name="upass"/><br/> <input type="submit" value="登录"/><br/> </form></body></body></html>
LoginController
@RequestMapping("loginResult.xhtml") public String loginResult(HttpServletRequest request){ String name = request.getParameter("uname"); String pass = request.getParameter("upass"); String result=null; //判断数据库 if("admin".equals(name)&&"123456".equals(pass)){ result="success"; }else{ result="fail"; } request.setAttribute("result", result); return "loginResult"; } @RequestMapping("login.xhtml") public String login(HttpServletRequest request){ return "login"; }
loginResult.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><body>${result }</body></html>
为了节约时间就不跑数据库了,用户密码直接写死
效果图
这里输入用户名admin,密码123456,返回的结果是success,错误则返回fail。
服务器写好了,接下来我们要在客户端(安卓)向服务器(WEB)提交用户名和密码,然后返回对应的结果
直接上代码,先是布局文件
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.zy.commitdatatoserver.MainActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:text="admin" android:id="@+id/et_main_uname" /> <EditText android:id="@+id/et_main_upass" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="123456" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="loginByGet" android:text="登录(GET)" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="loginByPost" android:text="登录(POST)" /></LinearLayout>
我们是向服务器提交数据,所以需要联网的权限。
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
MainActivity
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 java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;public class MainActivity extends AppCompatActivity { private EditText et_main_uname; private EditText et_main_upass; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_main_uname = (EditText) findViewById(R.id.et_main_uname); et_main_upass = (EditText) findViewById(R.id.et_main_upass); } public void loginByGet(View view) { String uname = et_main_uname.getText().toString(); String upass = et_main_upass.getText().toString(); String path = "http://192.168.42.253:8090/front/loginResult.xhtml"; //可变数组 new MyTask().execute(uname, upass, path, "GET"); } public void loginByPost(View view) { String uname = et_main_uname.getText().toString(); String upass = et_main_upass.getText().toString(); String path = "http://192.168.42.253:8090/front/loginResult.xhtml"; //可变数组 new MyTask().execute(uname, upass, path, "POST"); } class MyTask extends AsyncTask { private HttpURLConnection connection; private URL url; @Override protected Object doInBackground(Object[] objects) { //获取参数的值 String uname = objects[0].toString(); String upass = objects[1].toString(); String path = objects[2].toString(); String type = objects[3].toString(); String str="uname="+uname+"&upass="+upass; try { if ("GET".equals(type)) { //用GET方式提交 path = path + "?"+str; url = new URL(path); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(type); } else if ("POST".equals(type)) { //用POST方式提交 url = new URL(path); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(type); //设置contentType contentLength connection.setRequestProperty("Content-Length",str.length()+""); connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); //设置允许对外输出数据 connection.setDoOutput(true); //将用户名和密码提交到服务器 connection.getOutputStream().write(str.getBytes()); } connection.setConnectTimeout(5000); if (connection.getResponseCode() == 200) { InputStream is = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String result = br.readLine(); return result; } } 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(); } }}
GET和POST的提交方式最大的区别就在于GET是地址栏传参,而POST需要加入
//设置contentType contentLength
connection.setRequestProperty(“Content-Length”,str.length()+”“);
connection.setRequestProperty(“Content-Type”,”application/x-www-form-urlencoded”);
这两行代码来设置内容类型与长度
0 0
- Android向服务器提交数据
- Android向服务器提交数据
- Android--Post向服务器提交数据
- Android 实现Post向服务器提交数据
- Android之向服务器提交数据
- Android (向服务器提交数据)
- Android笔记:向服务器提交JSON格式的数据
- Android-使用GET、POST、AsyncHttpClient向服务器提交数据
- android 向服务器提交数据(get-post-AsyncHttpClient)
- Android 向服务器提交数据(GET-POST-AsyncHttpClient)
- Android向服务器提交数据(方式:get、post、AsyncHttpClient )
- Android之向服务器提交数据(POST,GET,AsyncHttpClient)
- Android 向服务器提交数据(GET-POST-AsyncHttpClient)
- Android (向服务器提交数据Get-Post)
- Android-向服务器提交数据(GET-POST-AsyncHttpClient)
- android之向服务器提交数据(GET,POST,AsyncHttpClient)
- Android向服务器提交文件
- 用Form向服务器提交数据
- select poll && epoll
- 关于32/64位的Oracle和32/64位的PL/SQL Developer匹配
- CMS垃圾回收器
- 突破github的100M单个大文件上传限制
- java中Math提供的是哪个取整的方法
- Android向服务器提交数据
- 各大公司Java后端开发面试题总结
- CodeForces 584A
- mysql存储过程 基本语法
- CodeForces 584B
- Maven内置隐式变量
- 5. Longest Palindromic Substring Medium
- 判断版本号的大小
- Qt5 中文乱码 以及C2001错误问题(换行、}这种)