用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
原创粉丝点击