HttpURLConnection
来源:互联网 发布:winsteel软件下载 编辑:程序博客网 时间:2024/06/06 05:30
请求头(get,post)
响应头(200 成功)–content-type(jpeg)mime—-content-length———服务端开发人员给我们的
响应正文—流文件
post
请求正文(提交给服务器的数据(流))
post请求 请求头多了两个数据content-type(jpeg)mime—-content-length,也是需要我们写给服务端开发人员
GET请求
package com.itheima.getmethod;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.net.URLEncoder;import com.itheima.htmlviewer.tools.Tools;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.app.Activity;import android.view.Menu;import android.view.View;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity { // Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { Toast.makeText(MainActivity.this, (String)msg.obj, 0).show(); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View v){ EditText et_name = (EditText) findViewById(R.id.et_name); EditText et_pass = (EditText) findViewById(R.id.et_pass); final String name = et_name.getText().toString(); final String pass = et_pass.getText().toString(); Thread t = new Thread(){ @Override public void run() { @SuppressWarnings("deprecation") String path = "http://169.254.244.136/Web2/servlet/Login?name=" + URLEncoder.encode(name) + //对要提交的表单数据进行url编码 "&pass=" + pass; try { URL url = new URL(path); //打开connection连接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //设置请求方法 conn.setRequestMethod("GET"); //设置连接超时时间 conn.setConnectTimeout(8000); //设置请求超时时间 conn.setReadTimeout(8000); if(conn.getResponseCode() == 200){ InputStream is = conn.getInputStream(); String text = Tools.getTextFromStream(is);//设置方法读取流数据 Message msg = handler.obtainMessage(); msg.obj = text; handler.sendMessage(msg); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; t.start(); }}
Tool类方法
package com.itheima.htmlviewer.tools;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;public class Tools { public static String getTextFromStream(InputStream is){ byte[] b = new byte[1024]; int len; ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { while((len = is.read(b)) != -1){ bos.write(b, 0, len); } //把字节数组输出流转换成字节数组,然后用字节数组构造一个字符串 String text = new String(bos.toByteArray()); return text; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }}添加网络权限
POST请求package com.itheima.postmethod;import java.io.InputStream;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.net.URLEncoder;import com.itheima.htmlviewer.tools.Tools;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.app.Activity;import android.view.Menu;import android.view.View;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity { Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { Toast.makeText(MainActivity.this, (String)msg.obj, 0).show(); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View v){ EditText et_name = (EditText) findViewById(R.id.et_name); EditText et_pass = (EditText) findViewById(R.id.et_pass); final String name = et_name.getText().toString(); final String pass = et_pass.getText().toString(); Thread t = new Thread(){ @Override public void run() { String path = "http://169.254.244.136/Web2/servlet/Login"; try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setConnectTimeout(8000); conn.setReadTimeout(8000); //添加post请求头中自动添加的属性 //流里的数据的mimetype conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); String content = "name=" + URLEncoder.encode(name) + "&pass=" + pass; //流里数据的长度 conn.setRequestProperty("Content-Length", content.length() + ""); //打开连接对象的输出流 conn.setDoOutput(true); //获取连接对象的输出流 OutputStream os = conn.getOutputStream(); //把数据写入输出流中 os.write(content.getBytes()); if(conn.getResponseCode() == 200){ InputStream is = conn.getInputStream(); String text = Tools.getTextFromStream(is); Message msg = handler.obtainMessage(); msg.obj = text; handler.sendMessage(msg); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; t.start(); }}
“`
0 0
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- httpurlconnection
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- HttpURLconnection
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- httpurlconnection
- 第九周项目二我的数组类
- JavaScript入门
- glclear glclearcolor小记
- Android-仿微信菜单
- 匈牙利算法和hopcroft_karp算法模板
- HttpURLConnection
- Android初级教程之内容提供者获取联系人信息
- 重识char与vchar
- 将uniy3D集成到android的子窗口中
- ARM 开发板insmod安装内核模块时,Unknown symbol usb_register_driver (err 0)
- 数据库6 关系代数(relational algebra) 函数依赖(functional dependency)
- 7、AngularJs的压缩问题
- request获取各种路径总结
- 仙剑奇侠传编年史(转自网络)