Android开发之HTTP访问网络
程序员文章站
2024-03-04 23:42:24
本文实例为大家详细介绍了android开发之http访问网络的相关代码,供大家参考,具体内容如下
代码1:
package com.ywhttpurlc...
本文实例为大家详细介绍了android开发之http访问网络的相关代码,供大家参考,具体内容如下
代码1:
package com.ywhttpurlconnection; import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.view; import android.widget.button; public class ywhttpurlconnectionactivity extends activity { /** called when the activity is first created. */ private button btn1 = null; private button btn2 = null; private button btn3 = null; private button btn4 = null; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); //直接获取数据 btn1 = (button) this.findviewbyid(r.id.button01); //get方式传递 btn2 = (button) this.findviewbyid(r.id.button02); //post方式传递 btn3 = (button) this.findviewbyid(r.id.button03); //获取图片 btn4 = (button) this.findviewbyid(r.id.button04); btn1.setonclicklistener(new button.onclicklistener(){ public void onclick(view v) { // todo auto-generated method stub intent intent = new intent(); intent.setclass(ywhttpurlconnectionactivity.this, showdata.class); bundle b = new bundle(); b.putint("id", 1); intent.putextras(b); startactivity(intent); } }); btn2.setonclicklistener(new button.onclicklistener(){ public void onclick(view v) { // todo auto-generated method stub intent intent = new intent(); intent.setclass(ywhttpurlconnectionactivity.this, showdata.class); bundle b = new bundle(); b.putint("id", 2); intent.putextras(b); startactivity(intent); } }); btn3.setonclicklistener(new button.onclicklistener(){ public void onclick(view v) { // todo auto-generated method stub intent intent = new intent(); intent.setclass(ywhttpurlconnectionactivity.this, showdata.class); bundle b = new bundle(); b.putint("id", 3); intent.putextras(b); startactivity(intent); } }); btn4.setonclicklistener(new button.onclicklistener(){ public void onclick(view v) { // todo auto-generated method stub intent intent = new intent(); intent.setclass(ywhttpurlconnectionactivity.this, showdata.class); bundle b = new bundle(); b.putint("id", 4); intent.putextras(b); startactivity(intent); } }); } }
代码2:
package com.ywhttpurlconnection; import java.io.bufferedreader; import java.io.dataoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.outputstream; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; import java.net.urlencoder; import android.app.activity; import android.content.intent; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.os.bundle; import android.util.log; import android.widget.imageview; import android.widget.textview; public class showdata extends activity { private textview tv = null; private imageview iv = null; private bitmap mbitmap = null; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.http); intent intent = this.getintent(); bundle b = intent.getextras(); int id = b.getint("id"); tv = (textview) this.findviewbyid(r.id.textview_http); iv = (imageview) this.findviewbyid(r.id.imageview01); //直接获取数据 if (id == 1) { // string httpurl = "http://192.168.0.132:8080/android/http.jsp"; string httpurl = "http://www.jb-aptech.com.cn"; url url = null; try { url = new url(httpurl); } catch (malformedurlexception e) { e.printstacktrace(); } if (url != null) { try { // 打开连接,此处只是创建一个实例,并没有真正的连接 httpurlconnection urlconn = (httpurlconnection) url.openconnection(); // 连接 urlconn.connect(); inputstream input = urlconn.getinputstream(); inputstreamreader inputreader = new inputstreamreader(input); bufferedreader reader = new bufferedreader(inputreader); string inputline = null; stringbuffer sb = new stringbuffer(); while ((inputline = reader.readline()) != null) { sb.append(inputline).append("\n"); } reader.close(); inputreader.close(); input.close(); urlconn.disconnect(); if(sb !=null){ tv.settext(sb.tostring()); }else{ tv.settext("读取的内容:null"); } } catch (ioexception e) { e.printstacktrace(); } }else{ log.i("tag", "url is null"); } }else if(id==2){ //get方式传递 // string httpurl = "http://192.168.0.132:8080/android/httpreq.jsp?par=hk"; string httpurl = "http://liveat.acewill.cn/liveat/?cmd=1&uid=xiaoming"; url url = null; try { url = new url(httpurl); } catch (malformedurlexception e) { e.printstacktrace(); } if (url != null) { try { httpurlconnection urlconn = (httpurlconnection) url .openconnection();// 打开连接,此处只是创建一个实力,并没有真正的连接 urlconn.setdoinput(true); urlconn.setdooutput(true); urlconn.connect();// 连接 inputstream input = urlconn.getinputstream(); inputstreamreader inputreader = new inputstreamreader(input); bufferedreader reader = new bufferedreader(inputreader); string inputline = null; stringbuffer sb = new stringbuffer(); while ((inputline = reader.readline()) != null) { sb.append(inputline).append("\n"); } reader.close(); inputreader.close(); input.close(); urlconn.disconnect(); if(sb !=null){ tv.settext(sb.tostring()); }else{ tv.settext("读取的内容:null"); } } catch (ioexception e) { e.printstacktrace(); } }else{ log.i("tag", "url is null"); } }else if(id==3){ //post方式传递 // string httpurl = "http://192.168.0.132:8080/android/httpreq.jsp"; string httpurl = "http://www.jb-aptech.com.cn"; url url = null; try { url = new url(httpurl); } catch (malformedurlexception e) { e.printstacktrace(); } if (url != null) { try { httpurlconnection urlconn = (httpurlconnection) url .openconnection();// 打开连接,此处只是创建一个实例,并没有真正的连接 urlconn.setdoinput(true); urlconn.setdooutput(true); urlconn.setrequestmethod("post"); urlconn.setusecaches(false);//post请求不能使用缓存. urlconn.setinstancefollowredirects(true);//是否自动重定向. urlconn.connect();// 连接 outputstream out = urlconn.getoutputstream(); dataoutputstream data = new dataoutputstream(out); data.writebytes("par="+urlencoder.encode("hk", "gbk")); data.flush(); data.close(); out.close(); inputstream input = urlconn.getinputstream(); inputstreamreader inputreader = new inputstreamreader(input); bufferedreader reader = new bufferedreader(inputreader); string inputline = null; stringbuffer sb = new stringbuffer(); while ((inputline = reader.readline()) != null) { sb.append(inputline).append("\n"); } reader.close(); inputreader.close(); input.close(); urlconn.disconnect(); if(sb !=null){ tv.settext(sb.tostring()); }else{ tv.settext("读取的内容:null"); } } catch (ioexception e) { e.printstacktrace(); } }else{ log.i("tag", "url is null"); } }else if(id==4){ string httpurl = "http://www.google.com.hk/intl/zh-cn/images/logo_cn.gif"; url url = null; try { url = new url(httpurl); } catch (malformedurlexception e) { e.printstacktrace(); } if (url != null) { try { // 打开连接,此处只是创建一个实例,并没有真正的连接 httpurlconnection urlconn = (httpurlconnection) url.openconnection(); urlconn.connect();// 连接 inputstream input = urlconn.getinputstream(); mbitmap = bitmapfactory.decodestream(input); if(mbitmap != null){ iv.setimagebitmap(mbitmap); } } catch (ioexception e) { e.printstacktrace(); } }else{ log.i("tag", "url is null"); } } } }
代码3:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ywhttpurlconnection" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="10" /> <uses-permission android:name="android.permission.internet"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".ywhttpurlconnectionactivity" > <intent-filter > <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".showdata"></activity> </application> </manifest>
代码4:http.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <textview android:id="@+id/textview_http" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <imageview android:id="@+id/imageview01" android:layout_width="172dp" android:layout_height="307dp" > </imageview> </linearlayout>
代码5.mail.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <button android:text="直接获取数据" android:id="@+id/button01" android:layout_width="fill_parent" android:layout_height="wrap_content"> </button> <button android:text="get方式传递" android:id="@+id/button02" android:layout_width="fill_parent" android:layout_height="wrap_content"> </button> <button android:text="post方式传递" android:id="@+id/button03" android:layout_width="fill_parent" android:layout_height="wrap_content"> </button> <button android:text="获取图片" android:id="@+id/button04" android:layout_width="fill_parent" android:layout_height="wrap_content"> </button> </linearlayout>
6.运行结果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。