提交数据(get,post)l两种方式

时间:2022-11-18 13:14:07
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >


    <EditText 
        android:id="@+id/et_username"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="请输入姓名"
        />
    <EditText 
        android:id="@+id/et_password"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码"
        />
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="doGet"
        android:text="Get方式提交"
        
        />
     <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="doPost"
        android:text="Post方式提交"
        
        />


</LinearLayout>


二:

package com.example.getwaydata;


import com.example.utils.NetUtils;


import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {
private EditText etUserName;
private EditText etPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etUserName = (EditText) findViewById(R.id.et_username);
etPassword = (EditText) findViewById(R.id.et_password);
}

public void doGet(View v){
final String userName = etUserName.getText().toString();
final String password = etPassword.getText().toString();
new Thread(new Runnable() {

@Override
public void run() {
//使用的是get抓取数据
final String state = NetUtils.loginOfGet(userName, password);
// 执行任务在主线程中
runOnUiThread(new Runnable() {
@Override
public void run() {
//就是在主线程中操作
Toast.makeText(MainActivity.this, state, 0).show();
}
});
}
}).start();
}

public void doPost(View v) {
final String userName = etUserName.getText().toString();
final String password = etPassword.getText().toString();
new Thread(new Runnable() {
@Override
public void run() {
final String state = NetUtils.loginOfPost(userName, password);
// 执行任务在主线程中
runOnUiThread(new Runnable() {
@Override
public void run() {
// 就是在主线程中操作
Toast.makeText(MainActivity.this, state, 0).show();
}
});
}
}).start();

}
}


三:

package com.example.utils;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;


import android.util.Log;


public class NetUtils {
private static final String TAG = "NetUtils";

/**
* 使用post的方式登录
* @param userName
* @param password
* @return
*/
public static String loginOfPost(String userName, String password) {
HttpURLConnection conn = null;
try {
URL url = new URL("http://10.0.2.2:8080/ServerItheima28/servlet/LoginServlet");

conn = (HttpURLConnection) url.openConnection();

conn.setRequestMethod("POST");
conn.setConnectTimeout(10000); // 连接的超时时间
conn.setReadTimeout(5000); // 读数据的超时时间
conn.setDoOutput(true);// 必须设置此方法, 允许输出
// conn.setRequestProperty("Content-Length", 234);// 设置请求头消息, 可以设置多个

// post请求的参数
String data = "username=" + userName + "&password=" + password;

// 获得一个输出流, 用于向服务器写数据, 默认情况下, 系统不允许向服务器输出内容
OutputStream out = conn.getOutputStream();
out.write(data.getBytes());
out.flush();
out.close();

int responseCode = conn.getResponseCode();
if(responseCode == 200) {
InputStream is = conn.getInputStream();
String state = getStringFromInputStream(is);
return state;
} else {
Log.i(TAG, "访问失败: " + responseCode);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(conn != null) {
conn.disconnect();
}
}
return null;
}


/**
* 使用get的方式登录
* @param userName
* @param password
* @return 登录的状态
*/
public static String loginOfGet(String userName, String password) {
HttpURLConnection conn = null;
try {
String data = "username=" + URLEncoder.encode(userName) + "&password=" + URLEncoder.encode(password);
URL url = new URL("http://192.168.1.118:8080/server/servlet/LoginServlet?" + data);
conn = (HttpURLConnection) url.openConnection();

conn.setRequestMethod("GET");// get或者post必须得全大写
conn.setConnectTimeout(10000); // 连接的超时时间
conn.setReadTimeout(5000); // 读数据的超时时间

int responseCode = conn.getResponseCode();
if(responseCode == 200) {
InputStream is = conn.getInputStream();
String state = getStringFromInputStream(is);
return state;
} else {
Log.i(TAG, "访问失败: " + responseCode);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(conn != null) {
conn.disconnect();// 关闭连接
}
}
return null;
}
/**
* 根据流返回一个字符串信息
* @param is
* @return
* @throws IOException 
*/
private static String getStringFromInputStream(InputStream is) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len=-1;
while((len=is.read(buffer))!=-1) {
baos.write(buffer, 0, len);
}
is.close();
//把六中的数据转化成字符串
String html = baos.toString();
return html;
}
}