This article gives us a detailed introduction of the Android development of the HTTP access network code, for your reference, the specific contents are as follows
Code 1:
Package com.ywhttpurlconnection;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Button; The public class Ywhttpurlconnectionactivity extends activity {/** called the ' when the ' is ' the ' activity ' is the ' the '. * 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);
Direct access to data BTN1 = (Button) This.findviewbyid (R.ID.BUTTON01);
Get Way passes BTN2 = (Button) This.findviewbyid (R.ID.BUTTON02);
Post Mode passes BTN3 = (Button) This.findviewbyid (R.ID.BUTTON03);
Get Picture Btn4 = (Button) This.findviewbyid (r.id.button04); Btn1.setonclicklistener (New Button.onclicklistener () {public void OnClick (View v) {//TODO auto-generated Meth
OD 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 Meth
OD 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 Meth
OD 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);
}
});
}
}
Code 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);
Direct FETCH data 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 {//open connection, where only one instance is created and there is no real connection httpurlconnection Urlconn = (httpurlconnection
) url.openconnection ();
Connecting 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 ("Read content: NULL");
} catch (IOException e) {e.printstacktrace ();
}}else{log.i ("TAG", "URL is null");
}else if (id==2) {//get pass//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 ();
Open the connection, here just to create a strength, and no real connection urlconn.setdoinput (true);
Urlconn.setdooutput (TRUE);
Urlconn.connect ();//connect InputStream input = Urlconn.getinputstream ();
InputStreamReader Inputreader = new InputStreamReader (input);
BufferedReader reader = new BufferedReader (Inputreader); StringInputline = 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 ("Read content: NULL");
} catch (IOException e) {e.printstacktrace ();
}}else{log.i ("TAG", "URL is null");
}else if (id==3) {//post pass//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 ();
Open the connection, where only one instance is created, and there is no true connection Urlconn.setdoinput (true);
Urlconn.setdooutput (TRUE); urLconn.setrequestmethod ("POST");
Urlconn.setusecaches (false);//post request cannot use cache.
Urlconn.setinstancefollowredirects (TRUE);//whether to automatically redirect.
Urlconn.connect ();//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 ("Read content: 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 {//open connection, where only one instance is created and there is no real connection httpurlconnection Urlconn = (httpurlconnection
) url.openconnection ();
Urlconn.connect ();//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");
}
}
}
}
Code 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= "/>
<uses-permission android:name=" Android.permission.INTERNET "/>
<application
android:icon=" @drawable/ic_launcher "
android: Label= "@string/app_name" >
<activity
android:label= "/app_name" @string 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>
Code 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:o" rientation= "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>
Code 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/he Llo "/> <button android:text=" Direct fetch data "android:id=" @+id/button01 "android:layout_width=" Fill_parent "Andro" id:layout_height= "Wrap_content" > </Button> <button android:text= "Get mode Pass" android:id= "@+id/button02" an Droid:layout_width= "Fill_parent" android:layout_height= "wrap_content" > </Button> <button android:text=
"Post-pass" android:id= "@+id/button03" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" > </Button> <button android:text= "Get Picture" android:id= "@+id/button04" android:layout_width= "Fill_parent" Andr oid:layout_height= "Wrap_content" > </button> </LinearLayout>
6. Operating Results
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.