采用get方式提交数据到服务器(无服务器)

    <uses-permission android:name="android.permission.INTERNET"/>
<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=".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="Button" />

</LinearLayout>
LoginService
package org.gentry.login.service;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.gentry.login.utils.StreamTools;

public class LoginService {
	/**
	 * 
	 * @param username
	 * @param password
	 * @return 返回null 登录异常
	 */
	public static String loginByGet(String username, String password) {
		// 提交数据到服务器
		try {
			String path = "http://192.168.1.100:8080/web/LoginServlet?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 = StreamTools.readInputStream(is);
				return text;
			} else {
				return null;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
}
StreamTools
package org.gentry.login.utils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class StreamTools {
	/**
	 * 把输入流的内容转化成字符串
	 * 
	 * @param is
	 * @return
	 */
	public static String readInputStream(InputStream is) {

		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			int len = 0;
			byte[] buffer = new byte[1024];
			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) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "获取失败";
		}
	}
}

MainActivity

package org.gentry.login;

import org.gentry.login.service.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().trim();
		final String password = et_password.getText().toString().trim();

		new Thread() {
			@Override
			public void run() {
				final String result = LoginService.loginByGet(username,
						password);
				if (result != null) {
					runOnUiThread(new Runnable() {
						public void run() {
							Toast.makeText(MainActivity.this, result,
									Toast.LENGTH_SHORT).show();
						}
					});
				} else {
					// 请求失败
					runOnUiThread(new Runnable() {
						public void run() {
							Toast.makeText(MainActivity.this, "请求失败",
									Toast.LENGTH_SHORT).show();
						}
					});
				}
			};
		}.start();
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
第一种方法 使用标准的JAVA接口 1 将用户名和密码等用Map泛型封装 再使用StringBuffer 转换成一串字符串 然后新建URL打开openConnection 得到 httpURLConnection 设置最长连接时间和setRequestMethod请求方法 用GET还是POST 提交数据用POST 打开输入和输出流 获取上传信息 字节大小以及长度 设置请求体的类型是文本类型 获得输出流 向服务器输出数据 获得服务器响应的结果和状态码 如果 返回码等于200 得到服务器返回的输入流 将输入流转换成指定编码的字符串并返回 就可以成功提交并得到服务器返回的信息 第二种方法 使用标准Apache接口 02 03 15 19 30 + 06 12 02 03 15 19 30 31 + 06 12 1 将用户名和密码等用Map泛型封装 再使用List<NameValuePair> list new ArrayList<NameValuePair> ; 转换成一串字符串 然后新建URL打开openConnection 得到 httpURLConnection 设置最长连接时间和setRequestMethod请求方法 用GET还是POST 提交数据用POST 打开输入和输出流 获取上传信息 字节大小以及长度 设置请求体的类型是文本类型 获得输出流 向服务器输出数据 获得服务器响应的结果和状态码 如果 返回码等于200 得到服务器返回的输入流 将输入流转换成指定编码的字符串并返回 就可以成功提交并得到服务器返回的信息">第一种方法 使用标准的JAVA接口 1 将用户名和密码等用Map泛型封装 再使用StringBuffer 转换成一串字符串 然后新建URL打开openConnection 得到 httpURLConnection 设置最长连接时间和setRequestMethod请求方法 用GET还是POST 提交数据用POST [更多]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值