Android and Web work together (Android reads website-specific pages)

Source: Internet
Author: User

Project ideas:

To develop a website with web development knowledge, you must read the content of this module through android.

Implementation:

  • You can use a mobile browser to directly read data. Advantages: you do not need to install a separate client. Disadvantage: some redundant information will consume the customer's traffic.
  • An app implemented through a website has the following advantages: you can design an app for a mobile client to filter out other images and irrelevant content to save customer traffic. Disadvantages: the development cost increases.

Now we use technology to implement the second method: implement the android client by yourself.

1. Create your own site

Release, update, and delete the content of your website on the Web Internet for maintenance. I believe that there are a lot of charges and no fees, and you can find your satisfaction by searching for the Internet. This time we will focus on Android apps.

2. Create a view

This view is simple, that is, you can view all the current text content.

<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="http://schemas.android.com/apk/res/android"               android:layout_width="306px" android:layout_height="410px"               android:layout_x="7px" android:layout_y="61px"             android:scrollbars="vertical" android:fadingEdge="vertical">     <TextView      android:id="@+id/ResultView"      android:layout_width="fill_parent"      android:layout_height="fill_parent"      android:layout_marginTop="2px"     android:layout_marginBottom="2px"     android:layout_marginLeft="2px"     android:layout_marginRight="2px"     android:text=""     android:layout_x="7px"     android:layout_y="61px"     >     </TextView></ScrollView>

3. Create an app Project (internetactivity)

Httpget is used this time, similar to httppost. The specific code is as follows:

public class InternetActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {                super.onCreate(savedInstanceState);        setContentView(R.layout.rdbox);        TextView rbox=(TextView)findViewById(R.id.ResultView);                HttpGet post=new HttpGet("http://www.demon.com/");        try{        HttpResponse response=new DefaultHttpClient().execute(post);        if(response.getStatusLine().getStatusCode()==200){                String result=EntityUtils.toString(response.getEntity());                result=result.replaceAll("<", "");                result=result.replaceAll(">", "");            rbox.setText(result);        }        else{            rbox.setText("code:"+response.getStatusLine().toString());        }        }        catch(Exception ex){            rbox.setText("error:"+ex.getMessage().toString());            ex.printStackTrace();        }            }    }

Run, an error is found, and the content cannot be returned normally. After searching, the android permission is very strict and a line of code should be added to androidmanifest. xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="qiyesite.android.readbox"    android:versionCode="1"    android:versionName="1.0" ><uses-permission android:name="android.permission.INTERNET" />

It is important to remember the location. You must put it in the first-level directory under manifest. It is best to run it on the previous page.

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.