用get方法传输数据到服务器

搭建一个后台服务器登录的用户名和密码:


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;

	@Override
	protected 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() {
						@Override
						public void run() {
							Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();

						}
					});
				} else {
					runOnUiThread(new Runnable() {
						@Override
						public void run() {
							Toast.makeText(MainActivity.this, "请求失败....", Toast.LENGTH_SHORT).show();

						}
					});
				}
			};
		}.start();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值