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

来源:互联网 发布:nvidia控制面板优化 编辑:程序博客网 时间:2024/04/28 21:51
    <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 blocke.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 blocke.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;@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().trim();final String password = et_password.getText().toString().trim();new Thread() {@Overridepublic 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