Android HttpURLConnection的用法
来源:互联网 发布:新开淘宝小店日志 编辑:程序博客网 时间:2024/05/17 07:02
@Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView textView = (TextView) findViewById(R.id.textView1); // String httpUri="htt p://192.168.1.1:image" String result = ""; URL url = null; try { url = new URL(httpUri); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (url != null) { try { // 使用HttpURLConnection打开连接 HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); urlConn.setConnectTimeout(10000);// 10s内连不上就断开 // 因为这个是post请求,设立需要设置为true // urlConn.setDoOutput(true); // urlConn.setDoInput(true); // 设置以POST方式 // urlConn.setRequestMethod("POST"); // Post 请求不能使用缓存 // urlConn.setUseCaches(false); // urlConn.setInstanceFollowRedirects(true); // 配置本次连接的Content-type,配置为application/x-www-form-urlencoded的 // urlConn.setRequestProperty("Content-Type", // "application/x-www-form-urlencoded"); // 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成, // 要注意的是connection.getOutputStream会隐含的进行connect。 urlConn.connect(); // DataOutputStream流 OutputStream outputStream = urlConn.getOutputStream();// 向服务器写入 DataOutputStream out = new DataOutputStream(outputStream);// DataOutputStream is objectOutputStream的子类,也可以用objectOutputStream类 // 要上传的参数 // String content = "name=gyf"; // 将要上传的内容写入流中 out.writeBytes(new String("name=gyf"));// 要是用objectOutputStream就是out.writeObject(content);//写入服务器的参数,但是放到内存中了 // content = "&wife=" + URLEncoder.encode("lyx", "gb2312"); // out.writeBytes(content); // 刷新、关闭 out.flush();// 真正的写过去了 out.close(); // 获服务器取数据 InputStream inputStream = urlConn.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader reader = new BufferedReader(inputStreamReader);// 读字符串用的。 String inputLine = null; // 使用循环来读取获得的数据,把数据都村到result中了 while (((inputLine = reader.readLine()) != null)) { // 我们在每一行后面加上一个"\n"来换行 result += inputLine + "\n"; } reader.close();// 关闭输入流 // 关闭http连接 urlConn.disconnect(); // 设置显示取得的内容 if (result != null) { textView.setText(result); } else { textView.setText("读取的内容为NULL"); } } catch (Exception e) { // TODO: handle exception } } }
0 0
- Android HttpURLConnection的用法
- Android入门教程 HttpURLConnection的用法 HTTP请求
- HttpURLConnection--HttpURLConnection的基本用法
- Android HttpURLConnection用法详解
- android HttpURLConnection用法详解
- android------HttpURLConnection用法
- HttpURLConnection类的用法
- HttpUrlConnection的联网用法
- HttpURLConnection的用法举例
- HttpURLConnection的一些用法
- HttpUrlConnection的get用法
- HttpUrlConnection的用法
- HttpURLConnection的用法
- HttpURLConnection的基本用法
- 关于HttpURLConnection的用法
- HttpURlConnection的用法
- Android HttpClient HttpUrlConnection用法总结
- HttpURLConnection和HttpClient的用法
- JS应用
- unity3d 代码动态添加,修改BoxCollider2D
- C/C++之函数指针
- mysql 导出表结构和表数据 mysqldump用法【自己留存】
- 基数排序【转载】
- Android HttpURLConnection的用法
- 清除记住的局域网共享密码
- 第二次实验
- netty4.0之TCP的server与client初探(与3.X版本变化)DEMO
- 计算机硬件常识:RAID以及硬RAID和软RAID的区别
- Android获取百度音乐的音乐下载地址和歌词下载连接
- NumberPicker使用
- 【Android】jar包Proguard混淆方法
- FFMPEG详解