Android Get PHP Web content

Source: Internet
Author: User
http://blog.csdn.net/kaiqiangzhang001/article/details/8350938

Http://www.kuqin.com/shuoit/20140108/337497.html

http://blog.csdn.net/lzz360/article/details/16887237

Http://blog.sina.com.cn/s/blog_5a48dd2d0100tw0u.html

Http://bbs.51cto.com/thread-954839-1.html

1. androidmanifest.xml join privileges:

permission Android:name= "Android. <> permission. INTERNET "/>

<>Permission Android:name= "Android. permission. Access_network_state "/>

2. android3.0 above all involves the network, the download and so on time operation, cannot run in the main thread, does not allow directly in the UI thread direct operation HttpClient

so method one: another thread to access.

Method Two: If you do not want to start another thread, add the following code, you can cancel the strict restrictions

Strictmode.threadpolicy policy=new StrictMode.ThreadPolicy.Builder (). Permitall (). build ();
Strictmode.setthreadpolicy (Policy);

3. Get the page content code:

Code One:

Package List.com.list; Import Org.apache.http.HttpResponse; Import Org.apache.http.client.methods.httpget;import Org.apache.http.impl.client.defaulthttpclient;import Org.apache.http.util.entityutils;import Org.json.jsonarray;import Android.app.Activity; Import Android.os.bundle;import Android.util.log;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.arrayadapter;import Android.widget.button;import Android.widget.ListView; public class ListActivityExtendsActivity{/** called when theActivityis first created. */public Button B = Null;public String s=null; Public ListView Listview1=null; @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.main); b = (Button) Findviewbyid (R.id.button1); listview1= (ListView) Findviewbyid (R.ID.LISTVIEW1); B.setonclicklistener (New Onclicklistener () {public void OnClick (View v) {//TODO auto-generated method stub HttpGet HTTPG ET = new HttpGet ("http://192.168.0.110:80/json/index.php"); HttpResponse Httpresponse;try {//execute gethttp commit HttpResponse = new Defaulthttpclient (). Execute (httpget); Httpresponse.getstatusline (). Getstatuscode () ==200) {//If successful, the returned data is converted to string type string s=entityutils.tostring ( Httpresponse.getentity ()); LOG.I ("JSON", s); Declares a JSON array Jsonarray js Jsonarray (s); Declare a data set that is as long as the length of his JSON array string[] data=new string[jsonarray.length ()]; //LoopsOutput for (int i=0;i
 
   
  
   arrayadapter=new arrayadapter 
   
    (List 
    Activity. This, Android. R.layout.simple_expandable_list_item_1,data);//Set ListView data; Listview1.setadapter (Arrayadapter);   }} catch ( 
    Exception e) {log.i ("E", E.getmessage (). toString ());}}); }}
 
   

  
 

Code two:</string>
New Thread () {public void run () {client = new defaulthttpclient (); StringBuilder builder = new StringBuilder (); HttpGet myget = new HttpGet ("http://10.0.2.2/testAndroid.php"); HttpGet myget = new HttpGet ("http://www.crazyit.org"); try {httpresponse response = Client.execute (Myget); httpentity entity = response.getentity (); BufferedReader reader = new BufferedReader (New InputStreamReader (Entity.getcontent ())); for (String s = reader.readline (); s! = null; s = Reader.readline ()) {builder.append (s);} Jsonobject jsonobject = new Jsonobject (builder.tostring ()); String Re_password = jsonobject.getstring ("password");  } catch (Exception e) {e.printstacktrace ();}}}. Start ();

Code Three
void GetInput () {   try  {  url url = new URL ("http://www.google.cn/");  HttpURLConnection conn = (httpurlconnection) url.openconnection ();  Conn.setdoinput (true);  Conn.setconnecttimeout (10000);  Conn.setrequestmethod ("GET");  Conn.setrequestproperty ("Accept", "*/*");  String location = Conn.getrequestproperty ("location");  int rescode = Conn.getresponsecode ();  Conn.connect ();  InputStream stream = Conn.getinputstream ();  Byte[] Data=new byte[102400];  int Length=stream.read (data);  String Str=new string (data,0,length);   Conn.disconnect ();  System.out.println (str);  Stream.Close ();  }  catch (Exception ee)  {  System.out.print ("EE:" +ee.getmessage ());   }  

The above describes the android to get PHP Web content, including aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.