与服务器进行数据Android--->servlet(get、post、AsyncClient)三种方式
来源:互联网 发布:淘宝开店考试题 编辑:程序博客网 时间:2024/05/22 00:44
Android端:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入用户名" android:id="@+id/et_main_uname" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" android:id="@+id/et_main_upass" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录(GET)" android:onClick="loginGet" /><Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录(Post)" android:onClick="loginPost" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录(AsyncHttpClient)" android:onClick="loginAsyncHttpClient" /></LinearLayout>
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); } //第一种:get public void loginGet(View view){ String uname=et_main_uname.getText().toString(); String upass=et_main_upass.getText().toString(); String path="http://193.168.2.141:7788/G160628_Server/login.do"; new MyGetTask().execute(uname,upass,path); } class MyGetTask extends AsyncTask{ @Override protected Object doInBackground(Object[] params) { String uname=params[0].toString(); String upass=params[1].toString(); String path=params[2].toString(); try { URL url=new URL(path+"?uname="+uname+"&upass="+upass); HttpURLConnection connection= (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); if(connection.getResponseCode()==200){ InputStream is=connection.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 public void loginPost(View view){ String uname=et_main_uname.getText().toString(); String upass=et_main_upass.getText().toString(); String path="http://193.168.2.141:7788/G160628_Server/login.do"; new MyPostTask().execute(uname,upass,path); } class MyPostTask extends AsyncTask{ @Override protected Object doInBackground(Object[] params) { String uname=params[0].toString(); String upass=params[1].toString(); String path=params[2].toString(); try { URL url=new URL(path); HttpURLConnection conn= (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setConnectTimeout(5000); //admin 123 //uname=admin&upass=123456 String s="uname="+uname+"&upass="+upass; //添加请求头 conn.setRequestProperty("Content-Length",s.length()+""); conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); conn.setDoOutput(true);//允许对外输出数据 OutputStream os=conn.getOutputStream(); os.write(s.getBytes()); if(conn.getResponseCode()==200){ InputStream is=conn.getInputStream(); BufferedReader br=new BufferedReader(new InputStreamReader(is)); String str=br.readLine(); return str; } } 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(); } } //第三种:AsycnClient(需要导jar包:android-async-http-1.4.4.jar,并且进行网络配置) public void loginAsyncHttpClient(View view){ String uname=et_main_uname.getText().toString(); String upass=et_main_upass.getText().toString(); String path="http://193.168.2.141:7788/G160628_Server/login.do"; AsyncHttpClient ahc=new AsyncHttpClient(); RequestParams params=new RequestParams(); params.put("uname",uname); params.put("upass",upass); ahc.post(this,path,params,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(); } }); }}
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.zking.administrator.g160628_android32_commitdata"><!--网络配置--> <uses-permission android:name="android.permission.INTERNET"></uses-permission> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
服务器端:<%@ 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><h1>登录页面</h1><form action="login.do" method="post">用户名:<input type="text" name="uname"/><br/>密码:<input type="password" name="upass"/><br/><input type="submit" value="登录"/><br/></form></body></html>
public class LoginServlet extends HttpServlet{@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//获取用户名和密码String uname=req.getParameter("uname");String upass=req.getParameter("upass");System.out.println(uname+" "+upass);String result=null;//判断if("admin".equals(uname) && "123".equals(upass)){result="success";}else{result="fail";}PrintWriter pw=resp.getWriter();pw.write(result);pw.close();}}
阅读全文
0 0
- 与服务器进行数据Android--->servlet(get、post、AsyncClient)三种方式
- Android网络数据之向服务器提交数据的三种方式(get+post+AsyncHttpClient)
- Android Get,Post,AsyncHttpClient向服务器提交数据三种方式
- Android向服务器提交数据(方式:get、post、AsyncHttpClient )
- Android 使用三种方式获取网页(通过Post,Get进行表单的提交)
- 数据传送三种方式(post、get、ajax)
- 使用get,post,httpclient三种方式向服务器提交文本数据
- 使用get,post,httpclient三种方式向服务器提交文本数据
- 使用get,post,httpclient三种方式向服务器提交文本数据
- android 通过get,post方式提交数据给服务器
- Android:向服务器提供数据之get、post方式
- Android 使用Post与Get方法进行表单提交数据
- Android Get,Post,AsyncHttpClient向服务器提交数据几种方式
- Android Get,Post,AsyncHttpClient向服务器提交数据几种方式
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- android中的http通信---(3)通过get、post方式传数据给服务器
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- HDU 6050 Funny Function
- HDU-6053 TrickGCD
- 洛阳hp:上课第二天
- IIS与.NET Framework的安装顺序问题
- 在windows中连接到虚拟机Linux的mysql
- 与服务器进行数据Android--->servlet(get、post、AsyncClient)三种方式
- intellj idea 自定义插件安装及使用
- 抽象类、接口、内部类和单例的总结!
- uva 10391 Compound Words
- node.js学习笔记之创建UDP服务器与客户端
- JVM Stacks
- PHP操作mongodb数据库操作类
- java-nio详细介绍
- [Poi2010]Monotonicity 2