代码:
import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import com.itheima.htmlview.utils.StreamTools; import android.app.Activity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText et_qq; private EditText et_pwd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_qq = (EditText) findViewById(R.id.et_qq); et_pwd = (EditText) findViewById(R.id.et_pwd); } public void login(View view){ final String qq = et_qq.getText().toString().trim(); final String pwd = et_pwd.getText().toString().trim(); if(TextUtils.isEmpty(qq) || TextUtils.isEmpty(pwd)){ Toast.makeText(this, "qq和密码不能空", 0).show(); return; }else{ new Thread(){ public void run() { try { //组拼参数到URL后面 String path = "http://192.168.22.136:8080/web/servlet/LoginServlet?username="+qq+"&password="+pwd; // 1、发送请求 // (1)创建一个URL对象 URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // (2)设置请求头信息 conn.setRequestMethod("GET"); conn.setConnectTimeout(3000); // 2、服务器返回数据 // (1)判断状态码:200 ok,404 没有找到资源、503、509 服务器端错误 int code = conn.getResponseCode(); if(code == 200){ // (2)解析服务器返回的二进制数据,解析成一个字符串 InputStream is = conn.getInputStream(); String result = StreamTools.readStream(is); System.out.println("---------"+ result); } } catch (Exception e) { e.printStackTrace(); } }; }.start(); } } } public class StreamTools { /** * 把输入流转换成一个字符串 * @param is * @return */ public static String readStream(InputStream is){ try { byte[] buffer = new byte[1024]; ByteArrayOutputStream baos = new ByteArrayOutputStream(); StringBuilder sb = new StringBuilder(); int len = -1; while((len = is.read(buffer)) != -1){ baos.write(buffer, 0, len); } return baos.toString(); } catch (Exception e) { return ""; } } } 使用POST方式提交数据(重点) 业务场景: 1、用户登录 2、文件上传1、设置请求头信息POST、Content-Length:
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //必须添加的两个请求头信息 conn.setRequestProperty("Content-Length", data.length()+""); conn.setRequestMethod("POST");2、设置把数据提交到服务器端:
conn.setDoOutput(true); //把数据写到服务器端 conn.getOutputStream().write(data.getBytes());缺点:
1、代码复杂优点:
1、安全; 2、提交大量的数据代码:
import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import com.itheima.htmlview.utils.StreamTools; import android.app.Activity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText et_qq; private EditText et_pwd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_qq = (EditText) findViewById(R.id.et_qq); et_pwd = (EditText) findViewById(R.id.et_pwd); } public void login(View view){ final String qq = et_qq.getText().toString().trim(); final String pwd = et_pwd.getText().toString().trim(); if(TextUtils.isEmpty(qq) || TextUtils.isEmpty(pwd)){ Toast.makeText(this, "qq和密码不能空", 0).show(); return; }else{ new Thread(){ public void run() { try { String path = "http://192.168.22.136:8080/web/servlet/LoginServlet"; String data = "username="+qq+"&password="+pwd; // 1、发送请求 // (1)创建一个URL对象 URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // (2)设置请求头信息 conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //必须添加的两个请求头信息 conn.setRequestProperty("Content-Length", data.length()+""); conn.setRequestMethod("POST"); conn.setConnectTimeout(3000); //设置把数据提交到服务器端 conn.setDoOutput(true); //把数据写到服务器端 conn.getOutputStream().write(data.getBytes()); // 2、服务器返回数据 // (1)判断状态码:200 ok,404 没有找到资源、503、509 服务器端错误 int code = conn.getResponseCode(); if(code == 200){ // (2)解析服务器返回的二进制数据,解析成一个字符串 InputStream is = conn.getInputStream(); String result = StreamTools.readStream(is); System.out.println("---------"+ result); } } catch (Exception e) { e.printStackTrace(); } }; }.start(); } } } get:提交数据大小是有限制 1Kb 4kb post: