用get方法传输数据到服务器
来源:互联网 发布:linux所有网卡的网关 编辑:程序博客网 时间:2024/05/16 07:54
搭建一个后台服务器登录的用户名和密码:
package com.zhangli.web;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@WebServlet("/Log")public class Log extends HttpServlet {private static final long serialVersionUID = 1L; public Log() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String username=request.getParameter("username");String password=request.getParameter("password");System.out.println("username="+username);System.out.println("password="+password);if("zhangsan".equals(username)&&"123".equals(password)){response.getOutputStream().write("login success".getBytes());}else{response.getOutputStream().write("login error".getBytes());}}}
新建android项目
布局中:
<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:orientation="vertical" tools:context="com.zhangli.Web.MainActivity" > <EditText android:id="@+id/et_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入用户名" /> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" android:inputType="textPassword" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click" android:text="登录" /></LinearLayout>
Utils:
package com.zhangli.utils;import java.io.ByteArrayOutputStream;import java.io.InputStream;public class Utils {public static String readInputStream(InputStream is) {ByteArrayOutputStream baos = new ByteArrayOutputStream();int len = 0;byte buffer[] = new byte[1024];try {while ((len = is.read(buffer)) != -1) {baos.write(buffer, 0, len);}is.close();byte[] result = baos.toByteArray();// 试着解析result里面的字符串String temp = new String(result);return temp;} catch (Exception e) {e.printStackTrace();return "获取失败";}}}
LogService:
package com.zhangli.LogService;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import com.zhangli.utils.Utils;public class LoginService {/** * * @param username * @param password * @return null 表示异常 text 表示成功 */public static String loginbyGet(String username, String password) {// 提交数据到服务器// 拼装路径try {String path = "http://192.168.0.14:8080/Log/Log?username=" + username + "&password=" + password;URL url = new URL(path);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setConnectTimeout(5000);conn.setRequestMethod("GET");int code = conn.getResponseCode();if (code == 200) {// 请求成功InputStream is = conn.getInputStream();String text = Utils.readInputStream(is);return text;} else {// 请求失败return null;}} catch (IOException e) {e.printStackTrace();}return null;}}
MainActivity:
package com.zhangli.Web;import com.zhangli.LogService.LoginService;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity {private EditText et_username;private EditText et_password;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);et_username = (EditText) findViewById(R.id.et_username);et_password = (EditText) findViewById(R.id.et_password);}public void click(View view) {final String username = et_username.getText().toString().intern();final String password = et_password.getText().toString().intern();new Thread() {public void run() {final String result = LoginService.loginbyGet(username, password);if (result != null) {runOnUiThread(new Runnable() {@Overridepublic void run() {Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();}});} else {runOnUiThread(new Runnable() {@Overridepublic void run() {Toast.makeText(MainActivity.this, "请求失败....", Toast.LENGTH_SHORT).show();}});}};}.start();}}
0 0
- 用get方法传输数据到服务器
- 用ssh方法scp,从本地传输文件到服务器
- android, httpclient,get,发送数据到服务器
- get / post 提交数据到服务器
- 通过GET方式传递 数据到服务器
- 客户端发送GET请求数据到服务器
- android,get发送数据到服务器,服务器验证登陆
- 采用get方式提交数据到服务器(无服务器)
- iOS传输Json数据(纯Json)到服务器
- 采用httpclient提交数据到服务器(get/post)(无服务器) → 采用get方式提交数据到服务器
- 服务器之间传输数据
- 用perl传输数据到指定ftp
- 笔记15--get/post提交数据到服务器
- Http Get 从服务器中获取数据 存储到本地
- android利用get提交数据到服务器进行验证
- Android 通过Get/Post提交请求数据到服务器
- Android中用get方式提交数据到服务器
- 使用Get方式提交数据到Tomcat服务器
- hdu 1874 畅通工程续(最短路)
- 斯普瑞斯奥特莱斯斯普瑞斯奥特莱斯董事长栾少梅欠债不还
- 根activity的启动过程--控制转移
- AngularJS中service,factory,provider的区别
- Java enum的用法详解
- 用get方法传输数据到服务器
- hdu 1166 敌兵布阵(线段树)
- java.net.UnknownHostException
- 关于git的常用方法,多账号以及一些规范(更新)
- java enum(枚举)使用详解 + 总结
- java枚举使用详解
- 单文档下Formview多窗口切换
- Java基础--进制、运算符、语句
- 构建java高效的缓存