http://192.168.22.136:8080/web/servlet/LoginServlet?username=123&password=1233
可以先用网页GET方式提交一次,用HttpWatch软件监视浏览器提交的内容,就可以看到详细的GET内容,
同样,POST方式提交的时候也可以得到POST提交时的内容
缺点: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 ""; } } }
业务场景:
1、用户登录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(); } } }