Android obtains php webpage content

Source: Internet
Author: User
: This article describes how to obtain php webpage content from android. For more information about PHP tutorials, see. 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. xmlAdd permission:

PermissionAndroid: name = "android. Permission. INTERNET "/>

PermissionAndroid: name = "android. Permission. ACCESS_NETWORK_STATE "/>

2. any time-consuming operations such as network and download cannot be run in the main thread or directly operate httpClient in the ui thread.

So Method 1: initiate another thread for access.

Method 2: If you do not want to start another thread and 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 webpage content code:

Code 1:

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 whenActivityIs 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 httpget = new HttpGet ("http: // 192.168.0.110: 80/json/index. php "); HttpResponse httpresponse; try {// execute gethttp to submit httpresponse = new defaulthttpclient(cmd.exe cute (httpget); if (httpresponse. getStatusLine (). getStatusCode () = 200) {// if the response is successful, convert the returned data to string type String s = EntityUtils. toString (httpresponse. getEntity (); Log. I ("JSON", s); // declare a json Array JSONArray js JSONArray (s); // declare a Data Group, the length of the json Array is the same as that of the String [] data = new String [jsonarray. length ()]; //LoopOutput for (int I = 0; I
  
   
Arrayadapter = new ArrayAdapter
   
    
(List
    Activity. This, android. R. layout. simple_expandable_list_item_1, data); // sets listview data; listview1.setAdapter (arrayadapter);} catch (
    ExceptionE) {Log. I ("E", e. getMessage (). toString ());}}});}}
   
  

Code 2:
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 3
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 how to get php webpage content from android, including related content. I hope my friends who are interested in PHP tutorials can help me.

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.